V
V
vladpenziy132020-04-15 18:21:00
PHP
vladpenziy13, 2020-04-15 18:21:00

How to retrieve data from API?

<?php
$curl =curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => " https://api-football-v1.p.rapidapi.com/v2/leagues ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS = > 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"x-rapidapi-host: api-football-v1.p.rapidapi.com",
"x-rapidapi- key: 3444c7f813mshf9250f3690d1a26p13dc53jsnd4589e46851a"
),
));

$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
How to display the league_id of the country England from the given code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-04-15
@vladpenziy13

$curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v2/leagues",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "x-rapidapi-host: api-football-v1.p.rapidapi.com",
                "x-rapidapi-key: 3444c7f813mshf9250f3690d1a26p13dc53jsnd4589e46851a"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            $response = json_decode($response);
            foreach ($response->api->leagues as $value) {
                if ($value->country == 'England') {
                    echo $value->league_id;
                }
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question