1

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>';

?>
Pacificano
  • 56
  • 7
  • 1
    You set `CURLOPT_HTTPHEADER => ['Accept: application/xml', 'user-key: XXXXXXXXXX']`, but use `json_decode` on reponse. Set it to `Accept: application/json`. – BitAccesser Jun 26 '16 at 03:14
  • Ahh crap. Good catch. Thats leftover from me trying everything to get it to work. I originally had it as application/json. Suffice to say changing it to xml doesnt make a difference. The array is still empty when put on live server. Appreciate the quick response. Cheers. – Pacificano Jun 26 '16 at 03:25
  • 1
    Are `PHP` warnings and errors displayed? Dump `$resp` and compare to localhost. Plz add the code to question (easier to read). Tried search for a known `entity_id` or other params? – BitAccesser Jun 26 '16 at 04:34
  • Ok, you're on to something. If I switch it to a known entity_id it works. `CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/restaurant?res_id=16774318'` returns an array for that restaurant. `CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/search?entity_id=16774318',` the search param returns a value with a known id. but `CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/search?q=jazushi'` search with a query doesn't work. only on my localhosts. would this suggest theres something wrong with their API and keyword searches? – Pacificano Jun 26 '16 at 06:16
  • Ok. Solved it. Thanks so much @BitAccesser ! Appreciate the help. `CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/search?q=jazushi'` returns an empty array. But if I add an entity_id, it now works perfectly. `CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/search?entity_id=260&entity_type=city&q=jazushi'` – Pacificano Jun 26 '16 at 06:24
  • 1
    What happens if you search for "jazushi" in documentation#!/restaurant/search on the zomato site? I can't test because generate API key is not working for me. – BitAccesser Jun 26 '16 at 06:36
  • searching for jazushi on the zomato api section works with just `https://developers.zomato.com/api/v2.1/search?q=jazushi`. works on zomato. works on localhost. doesnt work on my server. i'm confused why `?q=` works by itself on zomato and localhost but when its live it needs another parameter eg. `?entity_id=&q=`. so weird. – Pacificano Jun 26 '16 at 06:47
  • Is charset the same on both servers? Check `post-data` from `cURL` and compare with localhost. – BitAccesser Jun 26 '16 at 07:29

0 Answers0