dtncv04228 2017-03-21 23:41
浏览 67

如何在php中使用Yelp v3 Search API示例

I am currently using the Yelp API v3 and am testing some of their sample code. I have never used php before so it is quite confusing, but I am just wondering, where would I set the term and location equal to the users input in this script? Say I have an input for term and location in html, where in this script do I do $term = inputTerm and $location = inputLocation?

`#!/usr/bin/php
<?php
/**
* Yelp Fusion API code sample.
*
* This program demonstrates the capability of the Yelp Fusion API
* by using the Business 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/v3/documentation 
* for the API documentation.
* 
* Sample usage of the program:
* `php sample.php --term="dinner" --location="San Francisco, CA"`
*/
// OAuth credential placeholders that must be filled in by users.
// You can find them on
// https://www.yelp.com/developers/v3/manage_app
$CLIENT_ID = "abcd";
$CLIENT_SECRET =    "abcd";
// Complain if credentials haven't been filled out.
assert($CLIENT_ID, "Please supply your client_id.");
assert($CLIENT_SECRET, "Please supply your client_secret.");
// API constants, you shouldn't have to change these.
$API_HOST = "https://api.yelp.com";
$SEARCH_PATH = "/v3/businesses/search";
$BUSINESS_PATH = "/v3/businesses/";  // Business ID will come after slash.
$TOKEN_PATH = "/oauth2/token";
$GRANT_TYPE = "client_credentials";
// Defaults for our simple example.
$DEFAULT_TERM = "dinner";
$DEFAULT_LOCATION = "San Francisco, CA";
$SEARCH_LIMIT = 10;

function getQuery(){
echo $_GET['latlon'];
//echo $_GET['q'];
$query = $_GET['q'];
//break the query up into parts 
$first_space= strpos($query, " ");
$term = substr($query, 0, $first_space);
echo $term;

}
/**
* Given a bearer token, send a GET request to the API.
* 
* @return   OAuth bearer token, obtained using client_id and client_secret.
*/
function obtain_bearer_token() {
try {
    # Using the built-in cURL library for easiest installation.
    # Extension library HttpRequest would also work here.
    $curl = curl_init();
    if (FALSE === $curl)
        throw new Exception('Failed to initialize');
    $postfields = "client_id=" . $GLOBALS['CLIENT_ID'] .
        "&client_secret=" . $GLOBALS['CLIENT_SECRET'] .
        "&grant_type=" . $GLOBALS['GRANT_TYPE'];
    curl_setopt_array($curl, array(
        CURLOPT_URL => $GLOBALS['API_HOST'] . $GLOBALS['TOKEN_PATH'],
        CURLOPT_RETURNTRANSFER => true,  // Capture response.
        CURLOPT_ENCODING => "",  // Accept gzip/deflate/whatever.
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: application/x-www-form-urlencoded",
        ),
    ));
    $response = curl_exec($curl);
    if (FALSE === $response)
        throw new Exception(curl_error($curl), curl_errno($curl));
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if (200 != $http_status)
        throw new Exception($response, $http_status);
    curl_close($curl);
} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}
$body = json_decode($response);
$bearer_token = $body->access_token;
return $bearer_token;
}
/** 
* Makes a request to the Yelp API and returns the response
* 
* @param    $bearer_token   API bearer token from obtain_bearer_token
* @param    $host    The domain host of the API 
* @param    $path    The path of the API after the domain.
* @param    $url_params    Array of query-string parameters.
* @return   The JSON response from the request      
*/
function request($bearer_token, $host, $path, $url_params = array()) {
// Send Yelp API Call
try {
    $curl = curl_init();
    if (FALSE === $curl)
        throw new Exception('Failed to initialize');
    $url = $host . $path . "?" . http_build_query($url_params);
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,  // Capture response.
        CURLOPT_ENCODING => "",  // Accept gzip/deflate/whatever.
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "authorization: Bearer " . $bearer_token,
            "cache-control: no-cache",
        ),
    ));
    $response = curl_exec($curl);
    if (FALSE === $response)
        throw new Exception(curl_error($curl), curl_errno($curl));
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if (200 != $http_status)
        throw new Exception($response, $http_status);
    curl_close($curl);
} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}
return $response;
}
/**
* Query the Search API by a search term and location 
* 
* @param    $bearer_token   API bearer token from obtain_bearer_token
* @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($bearer_token, $term, $location) {
$url_params = array();

$url_params['term'] = $term;
$url_params['location'] = $location;
$url_params['limit'] = $GLOBALS['SEARCH_LIMIT'];

return request($bearer_token, $GLOBALS['API_HOST'], $GLOBALS['SEARCH_PATH'],   $url_params);
}
/**
* Query the Business API by business_id
* 
* @param    $bearer_token   API bearer token from obtain_bearer_token
* @param    $business_id    The ID of the business to query
* @return   The JSON response from the request 
*/
function get_business($bearer_token, $business_id) {
$business_path = $GLOBALS['BUSINESS_PATH'] . urlencode($business_id);

return request($bearer_token, $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) {     
$bearer_token = obtain_bearer_token();
$response = json_decode(search($bearer_token, $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($bearer_token, $business_id);

print sprintf("Result for business \"%s\" found:
", $business_id);
$pretty_response = json_encode(json_decode($response), JSON_PRETTY_PRINT |  JSON_UNESCAPED_SLASHES);
print "$pretty_response
";
}
/**
* User input is handled here 
*/
$longopts  = array(
"term::",
"location::",
);

$options = getopt("", $longopts);
$term = $options['term'] ?: $GLOBALS['DEFAULT_TERM'];
$location = $options['location'] ?: $GLOBALS['DEFAULT_LOCATION'];
//getQuery();
query_api($term, $location);
?>`
  • 写回答

1条回答 默认 最新

  • dongzhuang5741 2017-04-22 17:14
    关注

    Something like this will do that for you. The example doesn't take input filtering into consideration, so you'll want to mysql_real_escape the inputs first.

    // Defaults for our simple example.
    $DEFAULT_TERM           = "Dinner";
    $DEFAULT_LOCATION       = "White House, TN";
    $SEARCH_LIMIT           = 0;
    $REVIEWS                = 0;
    
    if(isset($_GET['term'])) $DEFAULT_TERM = $_GET['term'];
    if(isset($_GET['loc']) && $_GET['loc'] !='') $DEFAULT_LOCATION = $_GET['loc'] ;
    if(isset($_GET['limit'])) $SEARCH_LIMIT = $_GET['limit'];
    if(isset($_GET['reviews'])) $REVIEWS = $_GET['reviews'];
    

    And your form would look like this

    <form action="/" method="get">
        <input type="text" name="term" placeholder="search"/>
        <input type="text" name="loc" placeholder="zipcode"/>
        Results <select name="limit">
            <option value="3">3</option>
            <option value="5">5</option>
            <option value="7">7</option>
            <option value="9">9</option>
        </select>
        Show reviews <input type="checkbox" name="reviews" value="1"/>
        <input type="submit"/>
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题