1

How do I convert this result to a javascript array?

HTTP/1.1 200 OK Date: Sat, 02 Oct 2021 09:19:28 GMT Content-Type: application/json Content-Length: 796 Connection: keep-alive

{ "refresh_token_expires_in" : "7779999", "refresh_token_status" : "approved", "api_product_list" : "[trustpilot-client-api, public_data]", "app_enduser" : "APIID", "api_product_list_json" : [ "trustpilot-client-api", "public_data" ], "organization_name" : "trustpilot", "developer.email" : "[email protected]", "token_type" : "BearerToken", "issued_at" : "1633166368319", "client_id" : "CLIENTID", "access_token" : "ACCESSTOKEN", "refresh_token" : "TOKENNAME", "application_name" : "NAME6", "scope" : "", "refresh_token_issued_at" : "1633166368319", "expires_in" : "359999", "refresh_count" : "0", "status" : "approved" }

I thought it would be as simple as the PHP encoding it, then the Javascript using JSON.parse but I then get the error Uncaught SyntaxError: Unexpected token H in JSON at position 0

PHP Code:

$payload = http_build_query(array(
'grant_type' => 'password',
'username' => $username,
'password' => $password
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken'); //Url
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Basic ' .  base64_encode($apiKey . ':' . $secretKey),
    'Content-Type: application/x-www-form-urlencoded'
));

$content=curl_exec($ch);

if (curl_error($ch)) {
    $error_msg = curl_error($ch);
    echo json_encode($error_msg);
} else {
    
    echo json_encode($content);

}
curl_close($ch);

AJAX Code:

$.ajax({
    type: "POST",
    url: "api-test-access-token.php",
        dataType: "JSON",
        
        success: function (response, textStatus, xhr) {

            var jsonData = JSON.parse(response);
            console.log(response);


        }
    })

1 Answer 1

1

You need to set CURLOPT_HEADER above to false (or remove the line) as it will add the headers to the output of curl_exec and of course this is not a valid JSON. $content will then just contain the body of the response.

From the PHP documentation (see https://www.php.net/manual/en/function.curl-setopt.php):

CURLOPT_HEADER  |  true to include the header in the output.
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that's worked perfectly. You won't believe how long I've been trying different things with this - much appreciated!! StackOverflow should have the option so I can buy you a beer!!
Great to hear! Please mark this answer as accepted to help others to find the solution quicker.
All done mate, had to wait 3 minutes to trigger it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.