0

Trying to send a post request to the ServiceM8 Api however when i attempt the request i get back no errors and nothing adding to the ServiceM8 api.

Here is what servicem8 docs suggest:

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

and here is what i have:

$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 TO SHOW PREVIOUS ATTEMPTS TO RESOLVE BUT HAD NO LUCK..

<?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);
?>

adding the headers as i have in that example still does nothing and does not create the record in servicem8 or show an error.

Hopefully someone can help me make the correct Curl request using the PHP libary.

Thanks

Jrad51
  • 138
  • 1
  • 10
  • you forgot set header for reqest. – Naumov Jun 25 '16 at 21:31
  • i have tried this: $headers= array('Accept: application/json','Content-Type: application/json'); and then curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); but i get this error: Warning: curl_setopt(): You must pass either an object or an array with the CURLOPT_HTTPHEADER argument in C:\xampp\htdocs\final\sm8.php on line 16 – Jrad51 Jun 25 '16 at 21:35
  • yes header this is string dont array for example $header ="Content-type: text \r\n Acept: etc" I write from mobile I think you understand how to set header. – Naumov Jun 25 '16 at 21:41
  • @Naumov i have tried with the headers but still does nothing in servicem8, its like the request is failing and showing no errors back.. ignore my last error as i had the headers outside the function. – Jrad51 Jun 25 '16 at 21:42
  • @Naumov i dont quite understand due to being a total novice, Maybe when you get to a computer and not mobile, You could possible help me resolve the issue, it would be very much appreciated. Thanks – Jrad51 Jun 25 '16 at 21:52
  • You don't need to set `CURLOPT_CUSTOMREQUEST` to do a post with JSON data. Try getting rid of that and see what happens. Just set `CURLOPT_POST` to 1 and it should be fine. – drew010 Jun 25 '16 at 21:58
  • it still does not work i generally could cry!! its driving me insane, i am trying my damn best to follow the servicem8 docs but just cant seem to get the bloody thing working, i am useless when it comes to curl but i have managed to get my other apis working, Just struggling with servicem8.. the docs i am going from are: http://developer.servicem8.com/docs/rest-api/overview/ – Jrad51 Jun 25 '16 at 22:12

1 Answers1

1

First issue is it looks like you are not setting authentication details correctly. To use HTTP basic auth in CURL:

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

Second issue is that job_status is a mandatory field when creating Jobs, so you need to make sure to include that as part of your create request.

Assuming that you receive a HTTP 200 response, then you the UUID of the record you just created is returned in the x-record-uuid header (docs). See this answer for an example of how to get headers from a HTTP response in CURL.

Here's your example code modified to include the above advice:

$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>";
Community
  • 1
  • 1
telomere
  • 338
  • 3
  • 10