我通过PHP cURL 've built a site that connects to 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>";
?>
这在我的localhost上完美运行并返回完整的信息 . 当我在我的服务器上放置相同的代码时,它似乎连接到API ok但返回一个空数组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>';
?>