2016-02-28 75 views
0

我正在使用試圖在php中使用yelp中的API,使用示例代碼,但我甚至無法得到該工作。我只是想要一些方法來查看JSON或顯示示例輸入顯示什麼,所以我可以從那裏工作。這裏是示例代碼,我只是得到一個服務器錯誤500或當我嘗試查看源,一個空白頁。如何使用Yelp API PHP

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

?> 

回答

0

Error 500是內部服務器錯誤。嘗試使用更簡單的文件以確保服務器配置沒有問題。

樣品中的評論說這樣稱呼它:

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

這應該工作。

如果您在本地進行測試並且運行正常,然後嘗試將其移至遠程服務器,則使用建議的用法從遠程服務器的命令行(例如,通過SSH)嘗試它會很有幫助排除PHP解釋器的問題。

如果您嘗試以不同的方式使用它,那麼您可能需要修改腳本。例如,只需通過瀏覽器在網絡服務器上訪問它就可能不起作用,因爲termslocation將不會被設置。同樣以這種方式,作爲網頁運行時不需要第一行(#!/usr/bin/php)。

+0

是的,它運行時,我運行它,但是當我把它放在服務器上比試圖運行和查看源我不能查看JSON。 –

+0

如此運行不是作爲腳本,而是作爲網頁並通過瀏覽器訪問? –

+0

是的,我想從瀏覽器中查看它,因爲我最終希望將其用於Web應用程序。所以我只需要設置條款和位置? –