首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Curl PHP和ServiceM8发布JSON

通过Curl PHP和ServiceM8发布JSON
EN

Stack Overflow用户
提问于 2016-06-26 05:22:48
回答 1查看 173关注 0票数 0

尝试向ServiceM8 api发送post请求,但是,当我尝试发送请求时,我没有得到任何错误,也没有向ServiceM8 Api添加任何内容。

以下是servicem8文档的建议:

代码语言:javascript
复制
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

这就是我所拥有的:

代码语言:javascript
复制
$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;

-- UPDATE显示以前尝试解决但没有成功的尝试..

代码语言:javascript
复制
<?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请求。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-06-28 13:49:07

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

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

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

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

以下是修改后的示例代码,以包含上述建议:

代码语言:javascript
复制
$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>";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38033090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档