duanrou5680 2016-02-28 03:23
浏览 64

我如何使用Yelp API PHP

I am using trying to use the API from yelp in php, using the sample code, but I cant even seem to get that working. I just want some way to view the json or display what the sample inputs are displaying so I can work from there.Here is the sample code, I just get a server error 500 or when I try to view the source, a blank page.

  #!/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\"

",         
    count($response->businesses),
    $business_id
);

$response = get_business($business_id);

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

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

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

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

query_api($term, $location);

?>
  • 写回答

1条回答 默认 最新

  • duangengruan2144 2016-02-28 04:24
    关注

    Error 500 is an internal server error. Try with a simpler file to make sure the server config is not at fault.

    The comments in the sample say to call it like this:

    Sample usage of the program:
    php sample.php --term="bars" --location="San Francisco, CA"

    That should work.

    If you are testing it locally and it works, and then trying to move it to a remote server, it would be helpful to try it from the command line of the remote server (via SSH for example) using the suggested usage to rule out issues with the PHP interpreter.

    If you are trying to use it in a different way then you will likely need to modify the script. For example, simply accessing it on a webserver via a browser will likely not work because the terms and location will not be set. Also in this way the first line (#!/usr/bin/php) is not needed when ran as a web page.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站