1

我已经建立了一个站点,该站点通过 PHP cURL 连接到 Zomato 的 API https://developers.zomato.com/api,并以 json 格式检索一组信息。

我已经剥离了我在这里使用的代码:

<?php 

// Errors on
error_reporting(E_ALL);

// Get cURL resource
$curl = curl_init();

// Curl options
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Accept: application/json', 'user-key: XXXXXXXXXX'],
    CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/search?q=jazushi',
));

// Send the request & save response to $resp
$resp = curl_exec($curl);

// Check for errors if curl_exec fails
if(!curl_exec($curl)){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

// Close request to clear up some resources
curl_close($curl);

// dumping $resp
echo 'var_dump: <br>';
var_dump($resp);
echo '<br><br>';

// Decode json
$jsonZomato = json_decode($resp, true);

// print readable results
print "<pre>";
print_r($jsonZomato);
print "</pre>";

?>

这在我的本地主机上完美运行并返回完整的信息数组。当我将相同的代码放在我的服务器上时,它似乎可以连接到 API,但返回一个空数组http://i.imgur.com/ai7eJ0W.png

我已经检查了我的服务器上启用了 cURL,并且 PHP 版本几乎相同。我没有看到任何错误消息。如果我将伪造的 API 密钥放入请求中,Zomato 会向我发送错误消息。所以它似乎肯定连接正常。只是不检索任何进一步的信息。

我肯定对此感到头疼。任何帮助表示赞赏。干杯!

顺便说一句,如果这个问题没有得到解决并且其他人正在为此苦苦挣扎,我已经包含了一个非常基本的刮板,你可以使用它与 API 基本上做同样的事情。

<?php
    // scrape setup
    $rurl = 'https://www.zomato.com/sydney/jazushi-surry-hills';
    $contents = file_get_contents($rurl);

    // scrape the name
    preg_match('/h1 class="grey-text">(.*?)</s', $contents, $matches);
    $rname = $matches[1];

    // scrape the 'description'
    preg_match('/itemprop="priceRange" >(.*?)</s', $contents, $matches);
    $rcost = $matches[1];

    // scrape the phone number
    preg_match('/class="tel" itemprop="telephone">(.*?)</s', $contents, $matches);
    $rphone = $matches[1];

    // scrape the featured thumb
    preg_match('/meta property="og:image" content="(.*?)"/s', $contents, $matches);
    $rfeature = $matches[1];

    // converting to thumbnail
    $rthumb = substr($rfeature, 0, -6) . 'thumb' . substr($rfeature, -4);

    // scrape the address
    preg_match('/div class="resinfo-icon">(.*?)<\/div/s', $contents, $matches);
    $raddress = strip_tags(trim($matches[1]));

    // scrape the location
    preg_match('/" class="left grey-text fontsize3">(.*?)</s', $contents, $matches);
    $rlocation = trim($matches[1]);

    // scrape the cuisines
    preg_match('/a class="grey-text fontsize3" title="View all (.*?) in/s', $contents, $matches);
    $rcuisines = $matches[1];

    // scrape the rating
    preg_match('/property="zomatocom:average_rating" content="Rating: (.*?)"/s', $contents, $matches);
    $rrating = $matches[1];

    // scrape the lat
    preg_match('/itemprop="latitude" content="(.*?)" /s', $contents, $matches);
    $rlat = $matches[1];

    // scrape the lon
    preg_match('/itemprop="longitude" content="(.*?)" /s', $contents, $matches);
    $rlon = $matches[1];

    // scrape the opening hours
    $today = date('w');
    $days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

    for ($i=0; $i < count($days); $i++){
        // todays hours have a bold style
        if ($today === strval($i)) {
            preg_match('/>' . $days[$i]. '<\/div><div class="col-l-13 bold">(.*)<\/div>/iU', $contents, $    matches);    
        // every other da    y    
        } else {    
            preg    _match('/>' . $days[$i]. '<\/div><div class="col-l-13 ">(.*)<\/div>/iU', $contents, $    matches);    
        }    
        /    / make $hoursDay variables from $matches    
        $    {hours.$days[$i]} = $matches[1];    
    }    

    echo $rname . '<br>';
    echo $rphone . '<br>';
    echo $rfeature . '<br>';
    echo $rthumb . '<br>';
    echo $raddress . '<br>';
    echo $rlocation . '<br>';
    echo $rcuisines . '<br>';
    echo $rrating . '<br>';
    echo $rlat . '<br>';
    echo $rlon . '<br>';
    echo $rcost . '<br>';

?>
4

0 回答 0