Created
November 30, 2012 18:56
-
-
Save sidchilling/4177746 to your computer and use it in GitHub Desktop.
CURL method to make GET request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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