【问题标题】:Using curl to post an array to the godaddy api使用 curl 将数组发布到 godaddy api
【发布时间】:2019-06-08 15:11:35
【问题描述】:

我正在尝试将一堆域发布到 godaddy api,以获取有关定价和可用性的信息。但是,每当我尝试使用 curl 执行此请求时,我什么都没有返回。我仔细检查了我的密钥凭据,一切似乎都正确。我非常有信心问题出在格式化邮递区,我只是不知道该怎么做...谢谢任何可以提前提供帮助的人!

$header = array(
  'Authorization: sso-key ...'
);


$wordsArray = ['hello.com', "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($ch, CURLOPT_POST, true); //Can be post, put, delete, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $wordsArray);

$result = curl_exec($ch);  
$dn = json_decode($result, true);
print_r($dn);

【问题讨论】:

  • 谢谢,我会确保继续使用它们!

标签: php curl post request godaddy-api


【解决方案1】:

你的代码有两个问题:

  1. 发送数据的媒体类型必须为application/json(默认为application/x-www-form-urlencoded),并且您的PHP应用也必须接受application/json
$headers = array(
    "Authorization: sso-key --your-api-key--",
    "Content-Type: application/json",
    "Accept: application/json"
);
  1. 必须将发布字段指定为 JSON。为此,请使用 json_encode 函数:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));

完整的 PHP 代码是:

$headers = array(
    "Authorization: sso-key --your-api-key--",
    "Content-Type: application/json", // POST as JSON
    "Accept: application/json" // Accept response as JSON
);


$wordsArray = ["hello.com", "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));

$result = curl_exec($ch);

$dn = json_decode($result, true);
print_r($dn);

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多