简体   繁体   English

如何使用Yelp API PHP

[英]How do I use the 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. 我正在尝试使用示例代码从php中的yelp使用API​​,但是我似乎无法正常工作。 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. 我只想要一种查看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);

?>

Error 500 is an internal server error. Error 500是内部服务器错误。 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. 如果您正在本地对其进行测试并且可以正常工作,然后尝试将其移至远程服务器,则可以使用建议的用法从远程服务器的命令行(例如通过SSH)尝试使用它,这有助于解决问题。与PHP解释器。

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. 例如,仅通过浏览器在Web服务器上访问它就可能行不通,因为将不会设置termslocation Also in this way the first line ( #!/usr/bin/php ) is not needed when ran as a web page. 同样,当作为网页运行时,不需要第一行( #!/usr/bin/php )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM