doubihuai8468 2014-06-25 21:06
浏览 45
已采纳

Google App Engine(PHP) - 带有URL的file_get_contents返回false

I'm using google app engine locally and the request returns what it should.

Then I try on google app engine (the appspot service myapp.appspot.com) and the exact same call simply returns false without any errors.

The php documentation says I should get an error or a warning but I really don't see anything. http://www.php.net/manual/en/function.file-get-contents.php

I found this gae php documentation below but I really can't figure out what could be wrong. https://developers.google.com/appengine/docs/php/urlfetch/

I'm trying to fetch an url of another app from the appspot and the gae doc says:

Making requests to another App Engine app

If you are making requests to another App Engine app, you should consider telling the URL Fetch service to not follow redirects when invoking it.

Does anyone know how do I do that, or maybe what could even be the real problem ?

Here is my snippet:

    $content = file_get_contents($url, 0, $context);
    if (false === $content) {
        $error = error_get_last();
        throw new ClientException($error['message']);
    }

UPDATE:

Here is my php.ini:

google_app_engine.enable_functions = "php_sapi_name, php_uname, getmypid"
google_app_engine.allow_include_gs_buckets = "storage_bucket_name"
allow_url_include=1
allow_url_fopen=1

I tried with the following code:

    error_reporting(-1);
    ini_set('display_errors', 1);
    $content = file_get_contents($url, 0, $context);
    if (false === $content) {
        $error = error_get_last();
        throw new ClientException($error['message']);
    }

Now I get this error: "failed to open stream: Invalid headers. Must be a string."

This is my $context variable which is passed through stream_context_create(): http://www.php.net/manual/fr/function.stream-context-create.php

array(2) {
  ["http"]=>
  array(7) {
    ["method"]=>
    string(3) "GET"
    ["header"]=>
    string(0) ""
    ["content"]=>
    NULL
    ["protocol_version"]=>
    float(1)
    ["ignore_errors"]=>
    bool(true)
    ["max_redirects"]=>
    int(5)
    ["timeout"]=>
    int(5)
  }
  ["ssl"]=>
  array(1) {
    ["verify_peer"]=>
    bool(true)
  }
}

This is leading me to that old thread:

Using file_get_contents invalid headers issue on GAE

  • 写回答

1条回答 默认 最新

  • dragonlew9876 2014-07-03 14:29
    关注

    Since I had the error "failed to open stream: Invalid headers. Must be a string." I focused on the header part of the context.

    The $context['http']['headers'] was obviously a string(0) ""

    I tried to unset the $context['http']['headers'] if it was empty and the problem is solved.

    Note: this happens only in Google App Engine (GAE)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么