php - 无法通过 Curl 和 PHP 将数据发布到 API
问题描述
我正在使用 Blinksky Payments API。
下面是post参数
URI https://api.blinksky.com/api/v1
POST /send
{
"gift": {
"action": "order",
"apikey": "{APIKey}",
"sender": "Athens Automotive Inc.",
"from": "17705551234",
"dest": "16150001234",
"code": "62",
"amount": 100,
"postal": "30005",
"msg": "Thanks for taking our test drive!",
"reference": "{your_unique_order_id}",
"handle_delivery": false
}
}
当我在下面尝试我的代码时,它会引发错误
{ "statusCode": 404, "message": "Resource not found" }
我究竟做错了什么?下面是代码:
$url = "https://api.blinksky.com/api/v1";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$post ='{
"gift": {
"action": "order",
"apikey": "my-api-key-goes-here",
"sender": "Luxury Automotive Inc.",
"from": "17705551xxxxxxx",
"dest": "16150001xxxxxxx",
"code": "62",
"amount": 100,
"postal": "90211",
"msg": "Thanks for the Gift!",
"reference": "G62-786",
"handle_delivery": false
}
}';
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'
));
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
print_r($response);
解决方案
您的网址中缺少 /send 路径。改变这个:
$url = "https://api.blinksky.com/api/v1";
对此:
$url = "https://api.blinksky.com/api/v1/send";
推荐阅读
- javascript - 将“[]”字符串传输到 php 驱动的后端
- javascript - 如何在对象状态下删除对象数组反应
- javascript - Javascript (DAWG) 中的大量内存使用
- spring - 将 Spring Boot 2.0.8 升级到 2.1.2 - 无法在测试中加载 ApplicationContext
- python - Moviepy 设置 ImageClip 持续时间不起作用并且重复的剪辑被删除?
- mongodb - mongodb - 是否可以在查找查询中的 $elemMatch 投影之后进行过滤?
- node.js - Nodejs json.Stringify 函数不显示记录
- db2 - 我们什么时候应该在 DB2 中使用 NOT ENFORCED 和外键约束?
- android - 无法连接到 android.support.test.orchestrator.OrchestratorService
- java - 在 Grails 中使用复合键搭建表时出现 NullPointerException