Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading