如何使用cURL在php中调用Base crm REST api?

时间:2014-11-24 11:58:57

标签: php api rest curl

我在php中使用Base crm REST API时遇到问题。

Base crm为REST API here提供了一些代码

 curl -X POST -H "X-Pipejump-Auth:auth-token" \
-H "Accept:application/xml" \
-H "Content-Type:application/json" \
--data "{\"contact\" : { \"last_name\" : \"Barowsky\", \
  \"first_name\" : \"Foo\", \"is_organisation\" : \"false\" }}" \
  https://sales.futuresimple.com/api/v1/contacts/

现在可以请任何人帮助我如何在php中使用cURL。

我已经达到了这个目标:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/xml',
   'Accept: application/xml',
  'Connection: Keep-Alive' ));

curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:  "));

1 个答案:

答案 0 :(得分:2)

更新: Base已发布API V2(https://developers.getbase.com/)以及PHP库:https://github.com/basecrm/basecrm-php。我建议使用它而不是下面的代码片段。

已经有一段时间了,但让我们试一试。

我建议使用json而不是xml,用于请求和响应。

<?php

$token = "your_api_token";

$headers = array(
  "X-Pipejump-Auth: " . $token,
  "Content-Type: application/json",
  "Accept: application/json",
);

$curl = curl_init();
$url = "https://sales.futuresimple.com/api/v1/contacts.json";

$data = array(
  "contact" => array("first_name" => "My", "last_name" => "Contact")
);
$data_string = json_encode($data);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

$resp = curl_exec($curl);

curl_close($curl);

printf($resp);

?>