Sanjay Nakate Sanjay Nakate - 6 months ago 27
PHP Question

How to use Rest API curl command in php

I am using the clinch-pad rest api to create the contact using this document
Document rest api
I i am trying bellow snippet code to create contact on clinchpad but its getting error

We're sorry, but something went wrong.
.
so can any one tell me how do i go to achieve this.

$url = 'https://www.clinchpad.com/api/v1/contacts';
//$appId = 'YOUR_APP_ID';
$restKey = '676t547ndjdo784lk88dddh';
$headers = array(
"Content-Type: application/json",
//"X-Parse-Application-Id: " . $appId,
"X-Parse-REST-API-Key: " . $restKey
//"-u api-key:". $restKey
);
$objectData = '{
"_id": "521f2wwww6eccce8b4310e000076",
"name": "wwwwwwwwFoo Guy",
"designation": null,
"email": "fooguy@foocorp.com",
"phone": "5553331234",
"address": null,
"fields": [
{
"_id": "531ed3a49a21f6e90b00000e",
"name": "custom contact field",
"value": "Annual"
}
],
"organization": {
"_id": "sdfsdf5455sdfdf545455",
"name": "Foo Organization",
"email": "contact@foocorp.com",
"phone": "5553336666",
"website": "http://www.foocorp.com",
"address": "Foo City"
}
},
';
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_POST,1);
curl_setopt($rest,CURLOPT_POSTFIELDS,$objectData);
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($rest);
echo $response;
print_r($response);
curl_close($rest);

Answer

The data you're sending is not the valid JSON. The extra comma in the end is extra.

Is the $restKey in the example valid? I'm getting HTTP Basic: Access denied.

I guess you shouldn't put your private data (keys, codes, passwords) into the questions here.

Nevertheless I've made few changes and this should be working

$url = 'https://www.clinchpad.com/api/v1/contacts';  
 //$appId = 'YOUR_APP_ID';  
 $restKey = '676t547ndjdo784lk88dddh';  
 $headers = array(  
   "Content-Type: application/json",  
   // "X-Parse-Application-Id: " . $appId,  
//  "X-Parse-REST-API-Key: " . $restKey  
 //"-u api-key:". $restKey  
 );  
 $username = 'api-key';
 $password = $restKey;
 $objectData = '{
    "_id": "521f2wwww6eccce8b4310e000076",
    "name": "wwwwwwwwFoo Guy",
    "designation": null,
    "email": "fooguy@foocorp.com",
    "phone": "5553331234",
    "address": null,
    "fields":  [
        {
          "_id": "531ed3a49a21f6e90b00000e",
          "name": "custom contact field",
          "value": "Annual"
        }
    ],
    "organization": {
        "_id": "sdfsdf5455sdfdf545455",
        "name": "Foo Organization",
        "email": "contact@foocorp.com",
        "phone": "5553336666",
        "website": "http://www.foocorp.com",
        "address": "Foo City"
    }
    }';  
 $rest = curl_init();  
 curl_setopt($rest,CURLOPT_URL,$url);  
 curl_setopt($rest,CURLOPT_POST,1);  
 curl_setopt($rest,CURLOPT_POSTFIELDS,$objectData);  
 curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);  
 curl_setopt($rest, CURLOPT_USERPWD, $username . ":" . $password);  
 curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);  
 curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);  
 $response = curl_exec($rest);  
 echo $response;  
 print_r($response);  
 curl_close($rest);  

Main changes besides removing the comma are:

$username = 'api-key';
$password = $apiKey;
curl_setopt($rest, CURLOPT_USERPWD, $username . ":" . $password);