Search code examples
phpcurlgodaddy-api

Unexpected token error when integrating godaddy domain API


Error

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

Code

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

Solution

  • The issue is solved by the help of @Slaiv206 And below the working code.

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