0

I try to send a post request to an Google Api Endpoint, normaly a very easy thing to do. Unfortuatly the server is running PHP 5.4, and it seems like there is a bug (or am i crazy)

I pretty much used the same code as How do I send a POST request with PHP?

Everything used in that example is compatible with PHP >5.0

<?php
$url = 'https://myserver/echo.php';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

MyServer does simply show, the Post-Variables: echo "POST:\n"; var_dump($_POST);

If i execute that code on the server with PHP 5.4 the result is the following:

  POST: 
    array(2) { 
      ["key1"]=> string(6) "value1" ["amp;key2"]=> string(6) "value2" 
    }

My problem ist the "amp;key2", which is in my opinion a missbehavior of stream_context_create. It seems like it does not really skip the & (;amp) sign in the request parameters.

Is there any workaround for this issue? I cant force my client to change their webhoster because of that little issue.

Running the same code with PHP 5.5.19 works great.

Community
  • 1
  • 1
sellfisch
  • 21
  • 1

0 Answers0