Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post not working in file_get_contents client #96

Closed
ChristianRiesen opened this issue Oct 8, 2012 · 4 comments
Closed

Post not working in file_get_contents client #96

ChristianRiesen opened this issue Oct 8, 2012 · 4 comments

Comments

@ChristianRiesen
Copy link

I wanted to do a simple test. Send out a post to another location. It calls a file that is simply doing a print_r on $_POST.

$browser = new Buzz\Browser();
$response = $browser->post('http://test', array(), array('id' => 123, 'name' => 'tester'));

echo $response;

Returns
HTTP/1.1 200 OK
Date: Sat, 29 Sep 2012 03:48:57 GMT
Server: Apache
X-Powered-By: PHP/5.3.10-1ubuntu3.2
Vary: Accept-Encoding
Content-Length: 10
Connection: close
Content-Type: text/html

Array
(
)

$browser = new Buzz\Browser(new Buzz\Client\Curl());
$response = $browser->post('http://test', array(), array('id' => 123, 'name' => 'tester'));

echo $response;

Returns

HTTP/1.1 200 OK
Date: Sat, 29 Sep 2012 04:07:01 GMT
Server: Apache
X-Powered-By: PHP/5.3.10-1ubuntu3.2
Vary: Accept-Encoding
Content-Length: 47
Content-Type: text/html

Array
(
[id] => 123
[name] => tester
)

The inconsistency is in the handling of the request content on post. I can see curl is handling this with a switch, but there is no equivalent on file_get_contents. Is this a limitation of file_get_contents itself I'm unaware of or simply not fully implemented?

@lmammino
Copy link

I haven't dug on the code but I think this discussion may be helpful How to post data in PHP using file_get_contents?

@denisvmedia
Copy link

If we check the code:

public function post($url, $headers = array(), $content = '')
{
    return $this->call($url, RequestInterface::METHOD_POST, $headers, $content);
}

/**
 * Sends a request.
 *
 * @param string $url     The URL to call
 * @param string $method  The request method to use
 * @param array  $headers An array of request headers
 * @param string $content The request content
 *
 * @return MessageInterface The response object
 */
public function call($url, $method, $headers = array(), $content = '')
{
    // ...
}

We'll see that Browser::post() expects string and not array. Can this be the cause of the problem?

P.S. It seems that also only Curl client supports getFields() in the request object. And thus Browser::submit() will fail as well with any bundled client except Curl.

@egeloen
Copy link
Contributor

egeloen commented Aug 6, 2014

Posting an array of data is not possible with the file_get_contents adapter because the stream context does not support it only curl does. To fix it, you need to convert it to an url encoded format with the http_build_query method.

@kriswallsmith What about converting it when it is passed to stream_context_create in case it is an array? It should fix the issue mentionned by @denisvmedia about the Browser::submit method.

@Nyholm
Copy link
Collaborator

Nyholm commented Mar 17, 2018

Closing this since it is super old.

@Nyholm Nyholm closed this as completed Mar 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants