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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测