O'Reilly logo

Programming Social Applications by Jonathan LeBlanc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

GET Request

A GET request is one of the most straightforward request types. You are simply making a request to a URI endpoint and expecting some data to be returned to you.

In PHP, we can use a simple cURL request:

<?php
$url = 'http://www.example.com/request.php';

$ch = curl_init($url);
$options = array(
   CURLOPT_URL => $url,
   CURLOPT_RETURNTRANSFER => 1
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
?>

Within the cURL option, we are specifying that we want to call a specific URL (CURLOPT_URL) and would like to receive the response data from the request (CURLOPT_RETURNTRANSFER).

In Python, we can simply use urllib from the standard library:

import urllib

url = 'http://www.example.com/request.py'
f = urllib.urlopen(url)
response = f.read()

We open the specified URL and then read back the response from the request.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required