I've built a site that connects to Zomato's API https://developers.zomato.com/api via PHP cURL and retrieves an array of information in json.
I've stripped down the code I'm using to here:
<?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>";
?>
This works perfectly on my localhost and returns a full array of information. When I put the same code on my server, it seems to be connecting to the API ok but returns an empty array https://i.stack.imgur.com/OvF95.png
I've checked the cURL is enabled on my server and the PHP version is pretty much the same. I don't see any error messages. And if I put a bogus API key into the request, Zomato will send me an error. So it seems to definitely be connecting ok. Just not retrieving any further information.
I'm definitely over my head with this. Any help is appreciated. Cheers!
Btw if this doesn't get resolved and someone else is struggling with this, I've included a very rudimentary scraper that you can use which does basically the same thing as the 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>';
?>