Command Line cURL to PHP cURL Command Line cURL to PHP cURL curl curl

Command Line cURL to PHP cURL


You are passing the POST values as HTTP headers, which is not the same.

Use this:

$data = array('contact[first_name]' => 'aaaaaaa', 'api_token' => 'XXXXXXXXX');curl_setopt ($ch, CURLOPT_POST, true);curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);

And remove the CURLOPT_HTTPHEADER option.

Just as a side note: the linked API description says nothing about sending login details to the API - there's only the API token (which is usual for APIs).


example :

$url = 'link_to_api';$params = array('username'=>'admin','password'=>'passw0rd');    public function call_ws_post($url = '', $params = array()){    $service_url = $url;    $curl = curl_init($service_url);    $curl_post_data = $params;    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    curl_setopt($curl, CURLOPT_POST, true);    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);    $curl_response = curl_exec($curl);    if ($curl_response === false) {        $info = curl_getinfo($curl);        curl_close($curl);        die('error occured during curl exec. Additional info: ' . var_export($info));    }    curl_close($curl);    $decoded = json_decode($curl_response);    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {        die('error occured: ' . $decoded->response->errormessage);    }    return $decoded;}