首页 > 解决方案 > 无法通过 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);

标签: php

解决方案


您的网址中缺少 /send 路径。改变这个:

$url = "https://api.blinksky.com/api/v1";

对此:

$url = "https://api.blinksky.com/api/v1/send"; 

推荐阅读