Bik Byro Bik Byro - 1 year ago 88
PHP Question

PHP equivalent of Python's requests.get

I'm trying to access an API using PHP but for which the tutorial is only written in Python. The tutorial shows how to retrieve the data from a URL using

res = requests.get(API_URL, auth=(UID, SECRET))

Please can someone tell me what the equivalent statement would be in PHP, thanks.

Answer Source

EDIT: To add the UID and SECRET to file_get_contents, you need to add the variables like so:

$UID = "UserID";
$SECRET = "Secret";
echo file_get_contents(''.$UID.'&SECRET='.$SECRET);

So practically now the URL of file_get_contents will become:

You can use file_get_contents, like this:

<?php echo file_get_contents(''); ?>

Or this curl function:

function curl_get_contents($url)
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);

    return $data;
echo curl_get_contents('');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download