Home > OS >  Cant get data from Api in php, the api have the data in json format
Cant get data from Api in php, the api have the data in json format

Time:10-02

here is code example use both PHP built-in function file_get_contents() / CURL commands.

$api_url = 'https://api.tg3ds.com/api/v1/scan_records?apikey=1sjQKWfPpdyxRBvfv2BuTl5JzexOIScCFN0t&limit=20&offset=0&sort=scanned_at&user_id=PGLY1096&unfold=true&filter=PGLY1096';
// Read JSON file
$json_data = file_get_contents($api_url);
// Decode JSON data into PHP array
$response_data = json_decode($json_data);
var_dump($response_data);
exit();

As cURL

        // create & initialize a curl session
        $curl = curl_init();

        // set our url with curl_setopt()
        curl_setopt($curl, CURLOPT_URL, "https://api.tg3ds.com/api/v1/scan_records?apikey=1sjQKWfPpdyxRBvfv2BuTl5JzexOIScCFN0t&limit=20&offset=0&sort=scanned_at&user_id=PGLY1096&unfold=true&filter=PGLY1096");

        // return the transfer as a string, also with setopt()
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        // curl_exec() executes the started curl session
        // $output contains the output string
        $output = curl_exec($curl);
        var_dump($output);
        exit();

CodePudding user response:

For this curl call you have to set User Agent for example

`$context = stream_context_create( array( "http" => array( "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" ) ) );

$api_url = 'https://api.tg3ds.com/api/v1/scan_records?apikey=1sjQKWfPpdyxRBvfv2BuTl5JzexOIScCFN0t&limit=20&offset=0&sort=scanned_at> &user_id=PGLY1096&unfold=true&filter=PGLY1096'; $json_data = file_get_contents($api_url, false, $context);`

  • Related