2

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>

3 Answers 3

3

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(".

Sign up to request clarification or add additional context in comments.

Comments

1

Here's an example:

$json = '[
    {
        "ID": "1001",
        "Phone": "5555555555"
    }
]';

$jsonArray = json_decode($json);

foreach($jsonArray as $value){
    $id = $value->ID;
    $phone = $value->Phone;
}

5 Comments

oh really? because I just pulled this from my site which is using it, perhaps in my obfuscating the information I messed something up, care to point it out?
sandbox.phpcode.eu/g/d31ce.php - find it for yourself if you'Re programmer
oh, yeah... the quote marks aren't escaped in my example, I usually load the contents of this JSON remotely, so don't have to deal with it. I'll update the example.
Well, it does, but for the example of this post, I included a snippet of my JSON as if it existed in the same file and made a simple syntax mistake :)
0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.