2

I am trying to post a bunch of domains to the godaddy api in order to get information about pricing and availability. However, whenever I try to use curl to execute this request, I am returned nothing. I double checked my key credentials and everything seems to be right on that end. I'm pretty confident the issue is in formatting the postfield, I just don't know how to do that... Thank you to whoever can help in advance!

$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);
4

1 回答 1

2

您的代码中有两个问题:

  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);
于 2019-06-08T15:53:09.857 回答