4

I am using postmates API for my book store. I am using their test credentials, but I don't know how to use it. Kindly guide me how can I make a basic api call. I am using PHP. I want to send post data using cURL Thank you

<?php
  $url = "https://api.postmates.com/v1/customers/my-customer-key/delivery_quotes";
  $uname = "my-api-key";
  $pwd = null;
  $data = array(
        'dropoff_address' => '20 McAllister St, San Francisco, CA 94102',
        'pickup_address' => '101 Market St, San Francisco, CA 94105',
    );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pwd");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($process, CURLOPT_POST, 1);
    curl_setopt($process, CURLOPT_POSTFIELDS, $data);
    
        $output = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'error:' . curl_error($ch);
        }
        curl_close($ch);

        echo $output;
?>
  • Can you provide a link to 'postmates' to save people having to try to hunt it down? – Bobby Jack Sep 21 '15 at 10:51
  • Is it [this](https://postmates.com/developer/docs)? The documentation looks pretty good - is there a specific issue you're having problems with? – Bobby Jack Sep 21 '15 at 10:52
  • I am beginner and I want to know that, how can I authenticate myself on postmate? I am using cUrl to sent requests and I also set headers, but I am getting "invalid_authorization_error". Can please show me an example code in php? – Mohammad Tasneem Faizyab Sep 21 '15 at 11:09
  • More useful now you've given us some code. I *really* hope that isn't your actual API key, though! What is your actual, specific output? – Bobby Jack Sep 21 '15 at 11:31
  • Yes that was not my actual API key. Now I am getting this error "string(394) "HTTP/1.1 100 Continue HTTP/1.1 400 BAD REQUEST Content-Type: application/json Date: Mon, 21 Sep 2015 11:30:33 GMT Server: nginx/1.1.19 Content-Length: 205 Connection: keep-alive {"kind": "error", "code": "invalid_params", "params": {"dropoff_address": "This field is required.", "pickup_address": "This field is required."}, "message": "The parameters of your request were invalid."}" – Mohammad Tasneem Faizyab Sep 21 '15 at 11:38
  • 1
    Looks like "dropoff_address" and "pickup_address" are required parameters: https://postmates.com/developer/docs/endpoints#get_quote – Bobby Jack Sep 21 '15 at 12:03

2 Answers2

2

From the documentation:

The Postmates API requires authentication by HTTP Basic Auth headers. Your API key should be included as the username. The password should be left empty.

This means you need to do something like the following:

$curl = curl_init();
$url = "http://whatever-this-should-be";
curl_setopt($curl, CURLOPT_URL, $url);
$username = "your-api-key-goes-here";
$password = ""; // leave this blank, as per the doc
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$res = curl_exec($curl);

You'll need to tweak other curl options to handle output, etc. but this is the core of what you'll need.

You might also want to check out this library if you want to do all of this at a slightly higher (i.e. easier) level.

UPDATE: Please be aware that this is just sample code. I wouldn't advocate storing your API key in your script file for production code!

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
  • Thank you for this code. I have changed my code but now I am getting this error. string(394) "HTTP/1.1 100 Continue HTTP/1.1 400 BAD REQUEST Content-Type: application/json – Mohammad Tasneem Faizyab Sep 21 '15 at 11:34
2

I have made this code and it is working fine. Thank you all for answering my question.

    $url = "https://api.postmates.com/v1/customers/my-customer-id/delivery_quotes";

    $uname = "my-api-key";
    $pwd = null;

    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));
    curl_setopt($process, CURLOPT_HEADER, 1);
    curl_setopt($process, CURLOPT_USERPWD, $uname . ":" . $pwd);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_POST, 1);
    curl_setopt($process, CURLOPT_POSTFIELDS, "dropoff_address=20 McAllister St, San Francisco, CA 94102&pickup_address=101 Market St, San Francisco, CA 94105");
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
    $return = curl_exec($process);

    curl_close($process);

    var_dump($return);