我找到了一些關于使用 curl 的 post 和 get 方法的例子,但我無法處理服務器內部錯誤 500。谷歌控制臺報告了 fetch 型別的錯誤 500。我檢查了我的 php 配置,似乎沒問題。背景關系:基于elementor 模板的wordpress 安裝的functions.php 中的函式。Ionos 主機。我該如何除錯這個問題?
$url = 'https://myurl.com/';
$fields = array('var1' => 'value1', 'var2' => 'value2');
$headers = array('X-MY-TOKEN: tokenValue', 'Content-Type: application/json');
$fields_json = json.encode($fields);
//open connection
$ch = curl_init();
//setup
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_json);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
//execute post
$response = curl_exec($ch);

更新:php 日志錯誤顯示:PHP Fatal error: Uncaught Error: Call to undefined function encode()
How to send then a POST with application/json content-type?
uj5u.com熱心網友回復:
根據服務器拋出的錯誤,函式 encode() 找不到/不存在,它回傳 500 錯誤代碼。我假設您輸入錯誤 json_encode()。
所以應該是:
$fields_json = json_encode($fields);
從檔案。 https://www.php.net/manual/en/function.json-encode.php
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396434.html
標籤:php WordPress的 卷曲 元素
