2016-06-25 1 views
0

요청을 시도 할 때 ServiceM8 API에 대한 게시물 요청을 보내려고하지만 오류가없고 ServiceM8 API에 아무것도 추가하지 않습니다. 내가 무엇을ServiceM8 Curl을 통한 JSON 게시 PHP 및 API

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 

하고 여기에 있습니다 : : 여기

이 문서는 제안 servicem8 무엇

$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에서 레코드를 만들거나 전자 메일을 표시하지 않습니다. rror.

누군가가 PHP 라이브러리를 사용하여 올바른 Curl 요청을 할 수있게되기를 바랍니다.

감사합니다.

+0

:

다음은 위의 조언을 포함하도록 수정하여 예제 코드입니다. – Naumov

+0

나는 이것을 시도했다. $ headers = array ('Accept : application/json', 'Content-Type : application/json'); 그런 다음 curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); 하지만 난이 오류가 발생합니다 : 경고 : curl_setopt() : 줄 또는 16 행에 C : \ xampp \ htdocs \ final \ sm8.php에 CURLOPT_HTTPHEADER 인수가있는 배열을 전달해야합니다 – Jrad51

+0

예 머리글 이것은 string dont 배열입니다. 예 $ header = "Content-type : text \ r \ n Acept : etc"나는 모바일에서 씁니다. 헤더를 설정하는 방법을 이해하고 있다고 생각합니다. – Naumov

답변

1

첫 번째 문제는 인증 세부 정보를 올바르게 설정하지 않은 것 같습니다.

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

두 번째 문제는 작업을 만들 경우 job_status는 필수 필드입니다, 그래서 당신은 당신의 생성 요청의 일부로 포함되어 있는지 확인해야 CURL에서 HTTP 기본 인증을 사용합니다.

HTTP 200 응답을받은 것으로 가정하면 방금 작성한 레코드의 UUID가 x-record-uuid 헤더 (docs)로 반환됩니다. CURL의 HTTP 응답에서 헤더를 가져 오는 방법에 대한 예는 this answer을 참조하십시오. 당신이 reqest에 대한 헤더를 설정 깜빡

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