Get JSON data from API

Hi all,

I am trying to get json data from an API-set, and the code I got from the API-provider is like this:

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)

require_once 'HTTP/Request2.php';
 
$request = new Http_Request2('https://api.example.link/972412112');
$url = $request->getUrl();
 
$headers = array(
    // Request headers
    'Authorization' => 'Token YOUR_TOKEN',
                'Accept' => 'application/json',
);
 
$request->setHeader($headers);
 
$parameters = array(
    // Request parameters
);
 
$url->setQueryVariables($parameters);
 
$request->setMethod(HTTP_Request2::METHOD_GET);
 
// Request body
$request->setBody("{body}");
 
try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}
 
?>

I do not get any data in return.

Can You please guide me?

Best regards and thank You.

It appears you are missing (to begin with) two things:

1.You need to fill in your token which the provider will assign when you subscribe to their service
2. I am certain api.example.link is not a valid link

Hi Parkin T and thanks for the reply,

I have the Token and the correct Url. I have left them out in the example.

When I use the correct Token and Url I still do not get any data i return.
When I use the Token and Url in a Rest Client I get json data.

Best regards,
jmadsen

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.