如何在php中使用Rest API curl命令

时间:2016-05-10 13:21:56

标签: php api curl

我正在使用clinch-pad rest api使用本文档创建联系人 Document rest api 我正在尝试使用下面的代码片段来创建联系人,但是它的错误We're sorry, but something went wrong.。 所以任何人都可以告诉我如何实现这一目标。

$url = 'https://www.clinchpad.com/api/v1/contacts';  
 //$appId = 'YOUR_APP_ID';  
 $restKey = 'MY_API_KEY';  
 $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);  

1 个答案:

答案 0 :(得分:2)

您发送的数据不是有效的JSON。最后额外的逗号是额外的。

示例中的$ restKey是否有效?我收到HTTP Basic:拒绝访问。

我想你不应该把你的私人数据(密钥,密码,密码)放到这里的问题中。

尽管如此,我做了一些改动,这应该是有效的     

$url = 'https://www.clinchpad.com/api/v1/contacts';  
 //$appId = 'YOUR_APP_ID';  
 $restKey = 'MY_API_KEY';  
 $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);  

除删除逗号外的主要更改是:

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