Vin
Vin

Reputation: 89

Reading data from a private API

I found this private API for a game I have been playing which would allow me to create a pretty cool profile search website, however the API is structured a bit weird and I am not entirely sure how to call elements from this type of API.

{
"status": "success",
"id": "some_id_here",
"denormalized": {
    "some_url_here": {
        "data": {
            "created": "2019-01-10T04:19:21Z",
            "registered": 1547093961,
            "gender": "f",
            "display_name": "",
            "age": 23,
            "country": "US",
            "state": "NY",
        },  
    },
}

Above is the type of API that the platform has, it looks like as I scroll through it that it's just a private API for front end elements (cosmetic stuff) etc, I've excluded anything that could be personal.

Any way someone can help me figure out how to call from here? The portion which contains some_url_here is different for every unique user, I've done the following.

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "some_website_url",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$response = json_decode($response, true); //because of true, it's in an array
print '<pre>';
print_r($response);
print '</pre>';

but with that being said I can't get past the

[denormalized] => Array
        (
            [url-here] => Array
                (
                    [data] =>

portion to return the contents that come after [data]

Upvotes: 4

Views: 95

Answers (1)

Phil
Phil

Reputation: 164834

You can iterate the key / value pairs of denormalized using foreach. For example

foreach ($response['denormalized'] as $url => $value) {
    $data = $value['data'];

    $created = $data['created'];
    $registered = $data['registered'];
    // and so on
}

Demo ~ https://3v4l.org/AtXCQ

Upvotes: 3

Related Questions