Yelp API - 获取JSON结果

时间:2014-08-03 18:54:17

标签: php jquery json oauth yelp

我是JSON和Yelp的新手。我尝试使用表单搜索API。这是我的HTML:

<div>
    <form action="sample.php" method="post">
    <label>Term</label>&nbsp;<input name="term" id="term" type="text"/>
    <label>Location</label>&nbsp;<input name="location" id="location" type="text"/>
    <input id="submit" type="submit" name="submitRest" value="SUBMIT">
    </form>

    <div id="success"></div>

    <div id="result">Result</div>
</div>

我在这里通过ajax提交表单:

<script type="text/javascript">
    $('form').submit(function(evt){
        evt.preventDefault();
        var url = $(this).attr("action");
        var formData = $(this).serialize();
        $.ajax(url, {
            data : formData,
            type : "POST",
            success : function(response){
                $('#success').html("<p>It worked!</p><p>" + <?php $getYelpData ?> + "</p>")
            }
        });
    });
</script>

问题出在&#34; sample.php&#34;。它包括这个Oauth电话:

require_once('lib/OAuth.php');

    $Term = (isset($_POST['term'])) ? $_POST['term'] : '';
    $Location = (isset($_POST['location'])) ? $_POST['location'] : '';

    function getYelpData($Term, $Location) {
        $URL = "http://api.yelp.com/v2/search?term=" . $Term . "&amp;location=" . $Location; 

        $CONSUMER_KEY = -;
        $CONSUMER_SECRET = -;
        $TOKEN = -;
        $TOKEN_SECRET = -;

        $AuthToken = new OAuthToken($TOKEN, $TOKEN_SECRET);
        $Consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET);

        $AuthRequest = OAuthRequest::from_consumer_and_token(
            $Consumer, 
            $AuthToken, 
            'GET', 
            $URL
        );

        $AuthRequest->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $Consumer, $AuthToken);

        $Handle = curl_init($AuthRequest->to_url());
        curl_setopt($Handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($Handle, CURLOPT_HEADER, 0);
        $YelpResponse = curl_exec($Handle); // Yelp response
        curl_close($Handle);

        return json_decode($YelpResponse,true);
    }

如何从$ YelpResponse中获取JSON数据? ajax显示成功消息,因此我知道它正在工作,但我不确定如何从中获取我想要的数据。

0 个答案:

没有答案