Friday, October 28, 2022

[SOLVED] PHP Curl Fatal error on posting Json array on Localhost

Issue

I use the folowing Php/ Curl code to post an array of data to a webpage. When on my web hosting this is working fine without errors. When testing in local/xampp responds with the error:

Fatal error: Uncaught ValueError: curl_setopt_array(): Argument #2 ($options) must contain only valid cURL options;

curl_setopt_array(Object(CurlHandle), Array) #1 {main} thrown in C:\xampp\htdocs\test.php on line 88

I am looking everywhere but I can't find why this is not working on xampp/local and it has no issues on my hosting..

All help is welcome.

The code:

$PostCurrentAccInfo = json_encode($CurrentAccInfo);

    $ch = curl_init();
    curl_setopt_array($ch, array(
      CURLOPT_URL => 'https://external.site/UpdateAccounts.php',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_MAXREDIRS => 1,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_POST => true,
      CURLOPT_HEADER => 0,
      CURLOPT_HTTPHEADER, array('Content-Type: application/json'),
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $PostCurrentAccInfo, **// Line 88**
    ));
    $DatabaseResponse = curl_exec($ch);
    curl_close($ch);

Solution

you have an added the value as next array key instead of the value for CURL_HTTPHEADER:

CURLOPT_HTTPHEADER => array('Content-Type: application/json'),


Answered By - Gert B.
Answer Checked By - Terry (WPSolving Volunteer)