Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用YelpAPI PHP_Php_Json_Api_Yelp - Fatal编程技术网

如何使用YelpAPI PHP

如何使用YelpAPI PHP,php,json,api,yelp,Php,Json,Api,Yelp,我正在尝试使用php中的yelp的API,使用示例代码,但我甚至不能让它正常工作。我只是想用一些方法来查看json或显示示例输入显示的内容,这样我就可以从那里开始工作了 #!/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

我正在尝试使用php中的yelp的API,使用示例代码,但我甚至不能让它正常工作。我只是想用一些方法来查看json或显示示例输入显示的内容,这样我就可以从那里开始工作了

  #!/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);

?>
#/usr/bin/php

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

示例中的注释称其为:

程序的示例用法:
php sample.php--term=“bar”--location=“加利福尼亚州旧金山”

这应该行得通

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


如果您试图以不同的方式使用它,则可能需要修改脚本。例如,仅通过浏览器在Web服务器上访问它可能不起作用,因为不会设置
术语
位置
。同样,当作为网页运行时,也不需要第一行(
#!/usr/bin/php

是的,当我像那样运行它时,它可以工作,但当我把它放在服务器上,而不是尝试运行和查看源代码时,我无法查看json。因此,不是作为脚本运行,而是作为网页运行并通过浏览器访问?是的,我尝试从浏览器查看它,因为我最终想将其用于web应用程序。所以我只需要设置术语和位置?验证脚本的测试机器是否与Web服务器所在的物理机器相同?如果没有,请确保首先从命令行测试机器。。。