1

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?

CY1223
  • 41
  • 1
  • 7
  • 1) What does this command display `curl -i http://example.com/`? Use your domain name in the command. 2) Add logging: `syslog(LOG_WARNING, "Error:" . curl_error($ch));` 3) What messages are logged in Google Stackdriver? – John Hanley Oct 04 '19 at 20:15
  • Hi @JohnHanley Thanks for the reply. I'm abit new to Google Cloud platform but i followed your instructions to look at the messages logged in my Google Cloud console and I managed to found this error message: "PHP Fatal error: Call to undefined function curl_init() in /base/data/home/apps/f~cloud-assign...". And i googled a bit do i need to download something in order for the curl_init() function to work? – CY1223 Oct 05 '19 at 02:06
  • Are you using App Engine Standard? You will need to enable the cURL extension. If not, edit your question with more details on your Google Cloud services that you are using. Read these links: https://cloud.google.com/appengine/docs/standard/php/issue-requests AND https://cloud.google.com/appengine/docs/standard/php/runtime#dynamically_loadable_extensions – John Hanley Oct 05 '19 at 02:22
  • Yes i am using App Engine Standard since i'm just doing this for an assignment. And for the php.ini file does it have a predetermined format? or I just add this one line( extension = "curl.so" ) and push it to the cloud to enable the extention? – CY1223 Oct 05 '19 at 02:38

1 Answers1

2

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.

CY1223
  • 41
  • 1
  • 7
  • 1
    To improve your answer for others, add your `php.ini` and `app.yaml` files. These will serve as examples for others. – John Hanley Oct 05 '19 at 03:44