Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created November 30, 2012 18:56
Show Gist options
  • Save sidchilling/4177746 to your computer and use it in GitHub Desktop.
Save sidchilling/4177746 to your computer and use it in GitHub Desktop.
CURL method to make GET request
<?php
$url = 'http://localhost/tests/test_get'; # URL to the service
$data = array(
'first_name' => 'siddharth',
'last_name' => 'saha',
'city' => 'pune'
);
# Making the URL with the parameters to be sent with the request
$separator = '?';
foreach($data as $key => $value) {
if ($separator == '?') {
if (strpos($url, $separator) > 0) {
$separator = '&';
}
}
$url = $url . $separator . $key . '=' . $value;
}
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true); # Required to return output as string
$response = curl_exec($request);
curl_close($request);
if ($response === false) {
print "Could not make successful request\n";
} else {
$response = json_decode($response);
print $response->success . "\n";
print $response->first_name . "\n";
print $response->last_name . "\n";
print $response->city . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment