Reputation: 543
I'm trying to play with a web service via REST.
I finally am getting the results I want (or at least I think I am), but am unaware what to do with it. The response format is JSON.. I try outputting it via json_decode() to get it as an array, then I could do something with it.
You can see that I am getting "something" as a response as I am echoing the url that I am CURL'ing
I know this is a matter of education, but this is my first jaunt at this, so any help is appreciated. Again, my end goal is to obviously output the data in a readable format.
<?php
if(isset($_GET['word']))
{
$result= get_response_json($_GET['word']);
} else {$result = "";}
function get_response_json($word)
{
$postURL = "http://rhymebrain.com/talk?function=getRhymes&word=".urlencode($word);
echo $postURL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postURL);
curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
<html>
<title>Test Rhyme</title>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="get">
<input type="input" name="word" />
<input type="submit" value="Submit" />
</form>
<div id="results">
<?php
print_r(json_decode($result, true));
?>
</div>
</body>
</html>
Upvotes: 2
Views: 9988
Reputation:
Here's an example:
$json = '[
{
"ID": "1001",
"Phone": "5555555555"
}
]';
$jsonArray = json_decode($json);
foreach($jsonArray as $value){
$id = $value->ID;
$phone = $value->Phone;
}
Upvotes: 1
Reputation:
Here's a simplified way to do it without cURL
function get_response_json($word)
{
$postURL = "http://rhymebrain.com/talk?function=getRhymes&word=".urlencode($word);
$json = file_get_contents($postURL);
return $json;
}
Upvotes: 0
Reputation: 23939
Check here: http://php.net/manual/en/function.curl-exec.php. The one noteworthy thing I saw was this:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Note that there is a great example if you search for "curl_get(".
Upvotes: 3