ServiceM8通过Curl PHP和API发布JSON

时间:2016-06-25 21:22:48

标签: php json api post curl

尝试向ServiceM8 Api发送帖子请求但是当我尝试请求时,我没有收到任何错误,也没有添加到ServiceM8 api。

以下是servicem8文档的建议:

curl -u email:password 
-H "Content-Type: application/json"
-H "Accept: application/json" 
-d '{"status": "Quote", "job_address":"1 Infinite Loop, Cupertino, California 95014, United States","company_id":"Apple","description":"Client has requested quote for service delivery","contact_first":"John","contact_last":"Smith"}' 
-X POST https://api.servicem8.com/api_1.0/job.json

以下是我所拥有的:

$data = array(
  "username" => "**************8",
  "password" => "****************",
  "job_address" => "1 Infinite Loop, Cupertino, California 95014, United States"
);
$url_send ="https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);
function sendPostData($url, $post){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  $result = curl_exec($ch);
  return $result;

- 更新以显示以前尝试解决但是没有运气...

<?php
$data = array(
  "username" => "*******************",
  "password" => "**********",
  "job_address" => "1 Infinite Loop, Cupertino, California 95014, United States"
);

$url_send ="https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);

function sendPostData($url, $post){
$headers= array('Accept: application/json','Content-Type: application/json'); 
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  $result = curl_exec($ch);
  curl_close($ch);  // Seems like good practice
  return $result;
}
echo " " . sendPostData($url_send, $str_data);
?>

在该示例中添加标题仍然无效,并且不会在servicem8中创建记录或显示错误。

希望有人可以帮助我使用PHP库提供正确的Curl请求。

由于

1 个答案:

答案 0 :(得分:1)

第一个问题是看起来您没有正确设置身份验证详细信息。在CURL中使用HTTP基本身份验证:

curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

第二个问题是job_status在创建作业时是必填字段,因此您需要确保将其作为创建请求的一部分包含在内。

假设您收到HTTP 200响应,那么您在x-record-uuid标题(docs)中返回您刚刚创建的记录的UUID。有关如何从CURL中的HTTP响应中获取标头的示例,请参阅this answer

以下是您修改的示例代码,其中包含以上建议:

$data = array(
    "job_address" => "1 Infinite Loop, Cupertino, California 95014, United States",
    "status" => "Work Order"
);

$url_send = "https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);

function sendPostData($url, $post, $username, $password) {
    $ch = curl_init($url);
    if ($username && $password) {
        curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    }
    $headers = array('Accept: application/json','Content-Type: application/json');

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 1); // Return HTTP headers as part of $result
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $result = curl_exec($ch);

    // $result is the HTTP headers followed by \r\n\r\n followed by the HTTP body
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($result, 0, $header_size);
    $body = substr($result, $header_size);

    $strRecordUUID = "";
    $arrHeaders = explode("\r\n", $header);
    foreach ($arrHeaders as $strThisHeader) {
        list($name, $value) = explode(':', $strThisHeader);
        if (strtolower($name) == "x-record-uuid") {
            $strRecordUUID = trim($value);
            break;
        }
    }

    echo "The UUID of the created record is $strRecordUUID<br />";

    return $body;
}

echo "the response from the server was <pre>" . sendPostData($url_send, $str_data, $username, $password) . "</pre>";