2

I'm developping a google app using php. I need to consume REST service from Podio using the Podio-php API. But it uses cURL and I know it's not allowwed on GAE. So I tried tweaking the podio-php lib to use file_get_contents through a curl Emulator. Works fine locally, but when I deploy it, nothing works. I get this error message:

Warning: file_get_contents(https://api.podio.com:443/oauth/token): failed to open stream: Invalid headers. Must be a string. in /base/data/home/apps/s~wt-project1/9.374066902612513343/static/curlEmulator.php on line 167

And then, I get no responses and it breaks all the app.

Anyone has an idea from where the problems comes?

Here is the call:

$options = array( "ssl"=>array( "allow_self_signed"=>true, "verify_peer"=>false, ), 'http' => array( 'method' => $method, 'header' => $this->getValue(CURLOPT_HTTPHEADER), 'content' => $content ) ); $context = stream_context_create($options); file_get_contents($this->CURLOPT_URL, false, $context);

Thanks!

2 Answers2

1

I had the same problem on Google App Engine calling an HTTPS API.

I figured out that the problem was the headers format.

I switched from an array to a raw string as followed :

Before :

$base64ClientID = base64_encode(SANDBOX_CLIENT_ID . ":" . SANDBOX_SECRET_ID);
    $context_opts = array(
        "http" => array(
            "method" => "POST",
            "header" => array("Accept: application/json",
                              "Content-Type: application/x-www-form-urlencoded",
                              "Authorization: Basic " . $base64ClientID),
            "content" => "grant_type=client_credentials"
        ),
        "ssl" => array(
            "allow_self_signed" => true,
            "verify_peer" => false
        )
    );

    $context = stream_context_create($context_opts);

    $result = file_get_contents(...)

After:

$base64ClientID = base64_encode(SANDBOX_CLIENT_ID . ":" . SANDBOX_SECRET_ID);
    $context_opts = array(
        "http" => array(
            "method" => "POST",
            "header" => "Accept: application/json\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . $base64ClientID,
            "content" => "grant_type=client_credentials"
        ),
        "ssl" => array(
            "allow_self_signed" => true,
            "verify_peer" => false
        )
    );

    $context = stream_context_create($context_opts);

    $result = file_get_contents(...)

That solved all my problems.

Hope this will help.

Xavier V.
  • 6,068
  • 6
  • 30
  • 35
0

file_get_contents to my knowledge does not support HTTPS, as such it will see the handshake as invalid headers.

I'm unaware of what alternatives are available in GAE I'm afraid, but you should either look for a supported OAUTH library, or an alternative to file_get_contents that support HTTPS connections.

I'd suggest using the below code to find out whether or not HTTPS wrappers are available to you or not.

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);

Reference: above code referenced from this question

Community
  • 1
  • 1
Seidr
  • 4,946
  • 3
  • 27
  • 39
  • Thank you for your answer :) Unfortunately, CURL is not supported by GAE. And as it works locally on the app_engine sdk, it should work live doesn't it? – user3360950 Feb 27 '14 at 14:52
  • Right, I was about to add a comment saying 'I'm not sure if this will work on GAE or not', as I'm not aware of the limitations in GAE. – Seidr Feb 27 '14 at 14:52
  • It seems it is available : `openssl: yes http wrapper: yes https wrapper: yes wrappers: array(10) { [0]=> string(4) "ftps" [1]=> string(13) "compress.zlib" [2]=> string(4) "file" [3]=> string(4) "glob" [4]=> string(3) "ftp" [5]=> string(3) "zip" [6]=> string(3) "php" [7]=> string(4) "http" [8]=> string(5) "https" [9]=> string(2) "gs"}` – user3360950 Feb 27 '14 at 15:03
  • 1
    Are you sure that `$this->getValue(CURLOPT_HTTPHEADER)` contains a value? – Seidr Feb 27 '14 at 15:08
  • Ok, then I'm out of ideas at the moment - sorry. – Seidr Feb 27 '14 at 15:13