【问题标题】:Unexpected token error when integrating godaddy domain API集成godaddy域API时出现意外token错误
【发布时间】:2017-04-18 08:01:27
【问题描述】:

错误

{"message":"Unexpected token '","body":"{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl': 3600}"}

代码

$data = "{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}";

$url = "https://api.godaddy.com/v1/domains/{domain}/records";

$headers = array(
    'Content-Type: application/json',
    'Accept : application/json',
    'Authorization : sso-key {key}:{token}' );

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);

curl_close($curl);
print $response ;
exit;

【问题讨论】:

    标签: php curl godaddy-api


    【解决方案1】:

    问题在@Slaiv206的帮助下得到解决,并且在工作代码下方。

    $data = '[{ "type":"A", "name":"tarunDhiman", "data":"255.255.255.0", "ttl":3600 }]';
    
    $url = "https://api.godaddy.com/v1/domains/{domain}/records";
    
    $headers = array(
    	'Content-Type: application/json',
    	'Accept : application/json',
    	'Authorization : sso-key {key}:{secret}' );
    
    $curl = curl_init();
    
    curl_setopt($curl, CURLOPT_URL, $url);
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
    
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    print $response ;
    exit;

    【讨论】:

      【解决方案2】:

      我认为标题授权字符串是:

      Authorization: sso-key {KEY}:{SECRET}
      

      而不是:

      Authorization : sso-key {key}:{token}
      

      并且在 json 字符串中使用双引号代替单引号:

      '{ "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl":3600 }' 
      

      【讨论】:

      • 并且在 json 字符串中使用双引号而不是单引号: { "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl" :3600 }
      • 当使用 '{ "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl":3600 }' 时,双引号给出以下错误。 {"code":"INVALID_BODY","message":"请求正文不符合架构,详见fields","re​​sponseModel":"Error","fields":[{"message":"is不是数组","path":"records","code":"UNEXPECTED_TYPE"}]}
      • 查看文档,它需要在您发送的参数中包含一个数组
      • 是的,它需要一个数组,问题就解决了。我的错误是我在 josn 数据中使用了单句。谢谢朋友
      猜你喜欢
      • 2018-10-22
      • 2020-02-01
      • 1970-01-01
      • 2013-07-06
      • 2012-04-10
      • 2021-01-25
      • 2017-11-06
      • 1970-01-01
      • 2014-06-08
      相关资源
      最近更新 更多