0

cURL is new to me. I'm trying to integrate an api via PHP cURL. The api that I'm trying to access requires that parameters be sent as key-value pairs, not json. Their example cURL request in their docs is:

curl -i -X POST -d  'api_key=my_api_key' -d 
'[email protected]' -d "first_name=Joe" -d "last_name=Doe" -d 
"cust_id=cus_401" 
https://serviceurl.com/api/create

My code is apparently sending empty parameters to their api.

    $service_url = 'https://serviceurl.com/api/create';

    $curl = curl_init($service_url);

    $email = $this->session->userdata('email');

    $postArray = array(
        'api_key' => 'my_api_key',
        'email' => $email,
    );

    $curl_post_data = $postArray;
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);


    $curl_response = curl_exec($curl);
    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }
    curl_close($curl);
    echo $curl_response;
    echo $info;

Any advice would be greatly appreciated.

3
  • their example has more fields than your code - you have only used two - could that be the reason? Also - there is a subtle difference in the POST method - either application/x-www-form-urlencoded or multipart/form-data depending upon whether you use a simple array as the POST data or http_build_query to create the data string Commented Feb 28, 2018 at 7:17
  • The additional fields are not required. Just the api key and the email parameters. Could you expound on the difference in the POST method a little further. I don't quite understand. Thanks Commented Feb 28, 2018 at 7:35
  • php.net/manual/en/function.curl-setopt.php ~ look for CURLOPT_POSTFIELDS to read the description. Basically - If value is an array, the Content-Type header will be set to multipart/form-data so it depends upon whether the api is expecting urlencoded or multipart data Commented Feb 28, 2018 at 7:43

1 Answer 1

2

your curl php code is sending the data in multipart/form-data format, but as evident by their cli invocation example, their api wants the data in application/x-www-form-urlencoded format.

as explained by the curl_setopt docs, when you give CURLOPT_POSTFIELDS an array, it will automatically be encoded to multipart/form-data, if you give it a string, application/x-www-form-urlencoded will automatically be assumed, and is what their curl cli invocation is using.

lucky for you, PHP has a dedicated function for encoding arrays to application/x-www-form-urlencoded format, called http_build_query, thus curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data)); will fix your issue of apparently sending empty parameters.

also, if there is a problem setting any of your options, curl_setopt will return bool(false), which your code is completely ignoring, and would go unnoticed, you should fix that, consider using an error-catching setopt wrapper, like

function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . $ch .'. curl_error: '.curl_error($ch) );
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.