2

I am having trouble converting command line cURL to php cURL. I have read the other links on this topic, and am still stuck, and would appreciate any help.

I am trying to automatically put contact info from a web form into an online CRM.

API info: http://karmacrm.zendesk.com/entries/23648323-Contacts

My Code:

$header = array('contact[first_name]=aaaaaaa','API-KEY: XXXXXXXXX');
$pageurl = "https://app.karmacrm.com/api/v2/contacts.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pageurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERPWD, '[email protected]:7iNXXXXXXXXXX');
curl_setopt($ch, CURLOPT_HEADER, TRUE); // -i
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); // -v
curl_setopt($ch, CURLOPT_CUSTOMEREQUEST, "POST"); //-X
curl_setopt($ch, CURLOPT_BINARYTRANSFER, FALSE); // --data-binary, -d
$output = curl_exec($ch); 
curl_close($ch);
echo $output;

The Response:

HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Status: 401 
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.11 
Strict-Transport-Security: max-age=31536000 
X-UA-Compatible: IE=Edge,chrome=1 
Cache-Control: no-cache, private 
X-Request-Id: 8d9397e1c2d43580db7XXXXXXXXXXXX 
X-Runtime: 0.004153 
Date: Wed, 24 Apr 2013 03:14:27 GMT 
X-Rack-Cache: miss 
Server: nginx/1.2.3 + Phusion Passenger 3.0.11 (mod_rails/mod_rack)

{"error":"You need to sign in or sign up before continuing."}1

Thanks for any help, I appreciate it.

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, this was 100% correct, and fixed everything. Sorry I am amateur, Thanks again..
-1

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;
}

2 Comments

Can you explain why you think this will help compared to the current approach?
I think @eshepard set params as header post did not accurately

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.