邮递API不工作



我正在为我的书店使用postmates API。我在用他们的考试证书,但是我不知道怎么用。请指导我如何做一个基本的api调用。我正在使用PHP。我想使用cURL发送post数据谢谢你

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

来自文档:

Postmates API需要通过HTTP基本认证头进行身份验证。您的API密钥应该包含在用户名中。密码应为空。

这意味着你需要做一些的事情,像下面这样:

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

你需要调整其他curl选项来处理输出等,但这是你需要的核心。

如果你想在更高的(即更容易的)级别上完成所有这些工作,你可能也想检查一下这个库。

更新:请注意,这只是示例代码。我不提倡将API密钥存储在生产代码的脚本文件中!

我已经做了这个代码,它工作得很好。谢谢大家回答我的问题。

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

相关内容

  • 没有找到相关文章

最新更新