php - postman API 不工作

标签 php api curl postmates

我正在为我的书店使用 postmates API。 我正在使用他们的测试凭据,但我不知道如何使用它。请指导我如何进行基本的 api 调用。 我正在使用 PHP。我想使用 cURL 发送发布数据 谢谢

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

最佳答案

来自 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.

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

$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 选项来处理输出等,但这是您需要的核心内容。

您可能还想查看 this library如果您想在稍高(即更容易)的水平上完成所有这些。

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

关于php - postman API 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693275/

相关文章:

php - 在 laravel 中填写当前用户的 id 时出错

django - data._mutable= True 在 Django 休息框架

c++ - 有没有办法检测哪个应用程序获得焦点?

amazon-web-services - 使用 Curl(通过低级 API)将记录插入亚马逊的 DynamoDB

c++ - 将 CURL 转换为字符串时为空值

php - 在 Wordpress 中执行自定义字段

php - 添加类似查询的搜索时,查询结果发生变化

file - 如何在 Drupal 7 中将方案 ://filename. pdf 转换为相对路径?

curl - 无法转义引号和大括号

Php 比较字符串并返回常用值