我正在为我的书店使用 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/