Home > Blockchain >  what is the PHP equivalent for API Python's requests package
what is the PHP equivalent for API Python's requests package

Time:09-01

I am trying to replicate the same json request in php from this Python code:

import requests

url = 'http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard'
payload = {
    'limit':'500',
    'groups':'8'}

jsonData = requests.get(url, params=payload).json()

I know how to curl a API request in php but not sure the sythanx for the parameter input, in this case , 'limit' & 'groups'

CodePudding user response:

Use http_build_query() to convert the array of parameters to URL query parameters, then concatenate it to the URL.

file_get_contents() is the simple equivalent to requests.get() if you don't need to provide any custom headers.

$payload = [
    'limit' => 500,
    'groups' => 8
];
$params = http_build_query($payload);
$url = 'http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard?' . $params;
$result = file_get_contents($url);
$json_data = json_decode($result, true);
  • Related