Search code examples
phpgoogle-app-enginephp-curlapp.yamlzomato-api

Zomato API written in PHP not working in Google App Engine


Using the Zomato API I have written a simple PHP code to get the JSON data for the search query pizza. But for some reason it doesn't work when I push it to Google App Engine.

I have tried running the PHP code in my localhost using xampp and it works. But when I ran the code in Google cloud it shows me an empty page with no error messages.

This is the PHP code that I'm trying to run on the Google App Engine

  <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://developers.zomato.com/api/v2.1/search?q=pizza&start=0&count=10");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    $headers = array(
      "Accept: application/json",
      "User-Key: f0baf53bd8c31d3c625e9d9c0d379379"
      );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    echo "<pre>"; 
    print_r($result); 
    echo "</pre>";
    ?>

The app.yaml that I have written to push my code to Google cloud platform

runtime: php55
api_version: 1

handlers:
- url: /
  script: index.php

I am quite lost currently, hope some one can give me some insight to this problem

EDIT: Thanks to JohnHanley's comment below I have found the following error message shown my Google App Engine console: "PHP Fatal error: Call to undefined function curl_init() in /base/data/home/apps/f~cloud-assignment-2-254823/20191005t035406.421500489485193".

I have googled the error message apparently and found that apparently by default Google App Engine does not recognize the curl_init() function and apparently I need to enable it by adding a php.ini file and a write few lines of codes in the php.ini file before pushing it to the cloud. Sorry I am fairly new to this can someone show me how?


Solution

  • Thanks to John Hanley from the comments and a bit of googling I found this stack overflow post: app.yaml file mistake : Call to undefined function curl_init() which solved my problem.

    Apparently in default the cURL extension Google App Engine is disabled hence the error message saying the curl_init() function is not recognized and to activate it a php.ini needs to be created with the line "google_app_engine.enable_curl_lite = 1" inside and added to the folder with the app.yaml file before pushing it to the cloud.