我如何使用Yelp API PHP

时间:2016-02-28 03:23:14

标签: php json api yelp

我正在尝试使用示例代码在php中使用来自yelp的API,但我似乎无法使其正常工作。我只想要一些方法来查看json或显示示例输入显示的内容,这样我就可以在那里工作。这是示例代码,我只是得到服务器错误500或当我尝试查看源,一个空白页面。

  #!/usr/bin/php
 <?php

/**
 * Yelp API v2.0 code sample.
 *
 * This program demonstrates the capability of the Yelp API version   2.0
 * by using the Search API to query for businesses by a search term   and location,
* and the Business API to query additional information about the top result
* from the search query.
 * 
* Please refer to http://www.yelp.com/developers/documentation for the API documentation.
  \*  * This program requires a PHP OAuth2 library, which is included in this branch and can be
 * found here:
 *      http://oauth.googlecode.com/svn/code/php/
 * 
 * Sample usage of the program:
 * `php sample.php --term="bars" --location="San Francisco, CA"`
 */

// Enter the path that the oauth library is in relation to the php  file
require_once('auth.php');

// Set your OAuth credentials here  
// These credentials can be obtained from the 'Manage API Access'     page in the
// developers documentation (http://www.yelp.com/developers)
$CONSUMER_KEY = "GOTIT";
$CONSUMER_SECRET = "GOTIT";
$TOKEN = "GOTIT";
$TOKEN_SECRET = "GOTIT";


$API_HOST = 'api.yelp.com';
$DEFAULT_TERM = 'dinner';
$DEFAULT_LOCATION = 'San Francisco, CA';
$SEARCH_LIMIT = 3;
$SEARCH_PATH = '/v2/search/';
$BUSINESS_PATH = '/v2/business/';


/** 
 * Makes a request to the Yelp API and returns the response
 * 
 * @param    $host    The domain host of the API 
 * @param    $path    The path of the APi after the domain
 * @return   The JSON response from the request      
 */

function request($host, $path) {
$unsigned_url = "https://" . $host . $path;

// Token object built using the OAuth library
$token = new OAuthToken($GLOBALS['TOKEN'], $GLOBALS['TOKEN_SECRET']);

// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($GLOBALS['CONSUMER_KEY'], $GLOBALS['CONSUMER_SECRET']);

// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();

$oauthrequest = OAuthRequest::from_consumer_and_token(
    $consumer, 
    $token, 
    'GET', 
    $unsigned_url
);

// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);

// Get the signed URL
$signed_url = $oauthrequest->to_url();

// Send Yelp API Call
try {
    $ch = curl_init($signed_url);
    if (FALSE === $ch)
        throw new Exception('Failed to initialize');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);

    if (FALSE === $data)
        throw new Exception(curl_error($ch), curl_errno($ch));
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if (200 != $http_status)
        throw new Exception($data, $http_status);

    curl_close($ch);
} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}

   return $data;
}

/**
 * Query the Search API by a search term and location 
 * 
 * @param    $term        The search term passed to the API 
 * @param    $location    The search location passed to the API 
 * @return   The JSON response from the request 
 */
function search($term, $location) {
$url_params = array();

$url_params['term'] = $term ?: $GLOBALS['DEFAULT_TERM'];
$url_params['location'] = $location?: $GLOBALS['DEFAULT_LOCATION'];
$url_params['limit'] = $GLOBALS['SEARCH_LIMIT'];
$search_path = $GLOBALS['SEARCH_PATH'] . "?" . http_build_query($url_params);

return request($GLOBALS['API_HOST'], $search_path);
}

/**
 * Query the Business API by business_id
 * 
 * @param    $business_id    The ID of the business to query
 * @return   The JSON response from the request 
 */
function get_business($business_id) {
$business_path = $GLOBALS['BUSINESS_PATH'] . $business_id;

return request($GLOBALS['API_HOST'], $business_path);
    }

/**
 * Queries the API by the input values from the user 
 * 
 * @param    $term        The search term to query
 * @param    $location    The location of the business to query
 */
function query_api($term, $location) {     
$response = json_decode(search($term, $location));
$business_id = $response->businesses[0]->id;

print sprintf(
    "%d businesses found, querying business info for the top result \"%s\"\n\n",         
    count($response->businesses),
    $business_id
);

$response = get_business($business_id);

print sprintf("Result for business \"%s\" found:\n", $business_id);
print "$response\n";
}

/**
 * User input is handled here 
 */
$longopts  = array(
"term::",
"location::",
);

$options = getopt("", $longopts);

$term = $options['term'] ?: '';
$location = $options['location'] ?: '';

query_api($term, $location);

?>

1 个答案:

答案 0 :(得分:0)

Error 500是内部服务器错误。尝试使用更简单的文件来确保服务器配置没有错误。

示例中的评论说这样称呼:

  

程序的示例用法:
   php sample.php --term="bars" --location="San Francisco, CA"

这应该有效。

如果您在本地测试并且它正常工作,然后尝试将其移动到远程服务器,那么使用建议的用法从远程服务器的命令行(例如通过SSH)尝试它将会很有帮助排除PHP解释器的问题。

如果您尝试以不同的方式使用它,则可能需要修改脚本。例如,只需通过浏览器在网络服务器上访问它就可能无效,因为termslocation将不会设置。同样以这种方式运行网页时不需要第一行(#!/usr/bin/php)。