Question

When doing a POST request with 2 parameters:

return $this->perform($client->post("/authenticate",null, array(
  "username" => 'radupass',
  "password" => '@alcool', //starting with an @
)));

where $client:

/**
* @return \Guzzle\Service\Client
*/
public function getClient()
{
  if ($this->_client === null) {
    $this->_client = new Guzzle\Service\Client($this->baseUrl, $this->_options);
    $this->_client->setDefaultHeaders(array(
      "X-WebsiteId" => $this->websiteId,
      "X-AccessToken" => $this->accessToken,
    ));
}
return $this->_client;
}

I get the following exception:

2013/05/29 11:08:29 [error] [exception.Guzzle\Common\Exception\InvalidArgumentException] exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open lcool for reading' in /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Message/PostFile.php:58
Stack trace:
#0 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Message/PostFile.php(25): Guzzle\Http\Message\PostFile->setFilename('@lcool')
#1 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Message/EntityEnclosingRequest.php(245): Guzzle\Http\Message\PostFile->__construct('password', '@lcool', NULL)
#2 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Message/EntityEnclosingRequest.php(273): Guzzle\Http\Message\EntityEnclosingRequest->addPostFile('password', '@lcool', NULL, false)
#3 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php(127): Guzzle\Http\Message\EntityEnclosingRequest->addPostFiles(Array)
#4 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php(256): Guzzle\Http\Message\RequestFactory->create('POST', 'http://api.subs...', Object(Guzzle\Common\Collection), Array)
#5 /home/xxx/site/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php(336): Guzzle\Http\Client->createRequest('POST', '/authenticate', NULL, Array)
#6 /home/xxx/site/frontend/components/SubServerClient.php(117): Guzzle\Http\Client->post('/authenticate', NULL, Array)

Seems that the client is trying to open a file instead.

EDIT

Looking at the Guzzle source code I see in:

https://github.com/guzzle/guzzle/blob/master/src/Guzzle/Http/Message/RequestFactory.php#L88

 if (is_array($body) || $body instanceof Collection) {
                // Normalize PHP style cURL uploads with a leading '@' symbol
                foreach ($body as $key => $value) {
                    if (is_string($value) && substr($value, 0, 1) == '@') {
                        $request->addPostFile($key, $value);
                        unset($body[$key]);
                    }
                }
                // Add the fields if they are still present and not all files
                $request->addPostFields($body);
            }

What if I want to escape that character?

Was it helpful?

Solution

Seems to be an known issue with cURL itself:

Using CURL in PHP to post text data starting with "@"

One solution would be to use the percent encoding and '@' symbols would be '%40', even though that needs some extra work.

Also see this thread.

OTHER TIPS

When you take a look at Guzzle Request Documentation you can find that all POST parameters starting with @ sign is considered as a file path. See example from their documentation:

$request = $guzzleClient->post($url, $headers, [
    'file_field'   => '@/path/to/file.xml'
]);

I solved this by setPostField() method for assigning POST parameters, here is my example:

$request = $guzzleClient->post($url, $headers);
foreach ($postData as $key => $value) {
    $request = $request->setPostField($key, $value);
}
$guzzleClient->send($request);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top