簡體   English   中英

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

[英]How to use Rest API curl command in php

我正在使用clinch-pad rest api創建使用此文檔的聯系人Document rest api我正在嘗試下面的代碼段代碼在clinchpad上創建聯系人,但它出現錯誤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);  

您發送的數據不是有效的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); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM