簡體   English   中英

ServiceM8通過Curl PHP和API發布JSON

[英]ServiceM8 Post JSON via Curl PHP and API

嘗試將發布請求發送到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請求。

謝謝

第一個問題是您似乎未正確設置身份驗證詳細信息。 要在CURL中使用HTTP基本身份驗證:

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

第二個問題是在創建Jobs時job_status是必填字段,因此您需要確保將其包含在創建請求中。

假設您收到HTTP 200響應,那么您剛剛創建的記錄的UUID將返回到x-record-uuid標頭( docs )中。 請參閱此答案 ,以獲取有關如何從CURL中的HTTP響應中獲取標頭的示例。

這是修改后的示例代碼,以包含上述建議:

$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>";

暫無
暫無

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

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