I found some examples about post and get methods using curl, but I am not able to deal with the server internal error 500. The google console reports error 500 of fetch type. I checked my php configuration and seems to be ok. Context: function in functions.php of a wordpress installation based on elementor template. Ionos hosting. How can I debug this issue?
$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);
Update:the php log error shows: PHP Fatal error: Uncaught Error: Call to undefined function encode()
How to send then a POST with application/json content-type?
CodePudding user response:
Base on the error thrown by the server, the function encode() can't find/doesn't exist which returns a 500 error code. I assume you mistype the json_encode().
So it should be:
$fields_json = json_encode($fields);
From the docs. https://www.php.net/manual/en/function.json-encode.php