【问题标题】:ServiceM8 Post JSON via Curl PHP and APIServiceM8 通过 Curl PHP 和 API 发布 JSON
【发布时间】:2016-06-25 21:22:47
【问题描述】:

尝试向 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 请求。

谢谢

【问题讨论】:

  • 您忘记为请求设置标题。
  • 我试过这个: $headers= array('Accept: application/json','Content-Type: application/json');然后 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);但我收到此错误:警告:curl_setopt(): You must pass either a object or an array with the CURLOPT_HTTPHEADER argument in C:\xampp\htdocs\final\sm8.php on line 16
  • 是的,标题,这是字符串,不要数组,例如 $header ="Content-type: text \r\n Acept: etc" 我从手机上写我想你明白如何设置标题。
  • @Naumov 我已经尝试使用标头,但在 servicem8 中仍然没有执行任何操作,就像请求失败并且没有显示错误一样。忽略我的最后一个错误,因为我在函数之外有标头。
  • @Naumov 由于是个新手,我不太明白,也许当您使用计算机而不是移动设备时,您可以帮助我解决问题,将不胜感激。谢谢

标签: php json api post curl


【解决方案1】:

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

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

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

假设您收到 HTTP 200 响应,那么您刚刚创建的记录的 UUID 将在 x-record-uuid 标头 (docs) 中返回。有关如何从 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>";

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多