2

我无法将命令行 cURL 转换为 php cURL。我已阅读有关此主题的其他链接,但仍然卡住,希望能提供任何帮助。

我正在尝试将网络表单中的联系信息自动放入在线 CRM 中。

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

我的代码:

$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, 'mXXXX@gmail.com: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;

响应:

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

感谢您的帮助,我很感激。

4

2 回答 2

2

您将 POST 值作为 HTTP 标头传递,这是不一样的。

用这个:

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

并删除该CURLOPT_HTTPHEADER选项。

顺便说一句:链接的 API 描述没有说明向 API 发送登录详细信息 - 只有 API 令牌(这对于 API 来说很常见)。

于 2013-04-24T03:46:54.157 回答
-1

例子 :

$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;
}
于 2013-04-24T03:57:26.273 回答