Created
March 30, 2013 10:31
-
-
Save bradfeehan/5276257 to your computer and use it in GitHub Desktop.
Test for Guzzle PUT request with post fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Read 171 bytes from socket. | |
Full response: | |
BEGIN | |
### | |
PUT / HTTP/1.1 | |
Transfer-Encoding: chunked | |
Host: 127.0.0.1:8901 | |
User-Agent: Guzzle/3.3.1 curl/7.24.0 PHP/5.3.15 | |
Content-Type: application/x-www-form-urlencoded | |
0 | |
### | |
END | |
What Guzzle says it's going to send for this request: | |
BEGIN | |
### | |
PUT / HTTP/1.1 | |
Host: 127.0.0.1:8901 | |
User-Agent: Guzzle/3.3.1 curl/7.24.0 PHP/5.3.15 | |
Content-Type: application/x-www-form-urlencoded | |
my_post_param=my_value | |
### | |
END | |
Done. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Use composer autoloader | |
require_once 'vendor/autoload.php'; | |
// Create a service client pointing at a particular port locally | |
$client = \Guzzle\Service\Client::factory(array( | |
'base_url' => 'http://127.0.0.1:8901', | |
)); | |
// Create service description, including a PUT operation, which has | |
// some parameters with a "location" of "postField" | |
$client->setDescription(new Guzzle\Service\Description\ServiceDescription(array( | |
'operations' => array( | |
'MyCommand' => array( | |
'httpMethod' => 'PUT', | |
'parameters' => array( | |
'my_post_param' => array( | |
'location' => 'postField', | |
'type' => 'string', | |
), | |
), | |
), | |
), | |
))); | |
// Tell Guzzle client to not wait for any response to requests | |
$client->addSubscriber(new \Guzzle\Plugin\Async\AsyncPlugin()); | |
// Set up a socket to listen on, this will receive Guzzle's raw request | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Error creating socket: " . socket_strerror(socket_last_error())); | |
socket_bind($socket, '127.0.0.1', 8901) or die("Error binding socket: " . socket_strerror(socket_last_error())); | |
socket_listen($socket) or die("Error listening on socket: " . socket_strerror(socket_last_error())); | |
// Execute Guzzle command with a value for our post param | |
$client->MyCommand(array('my_post_param' => 'my_value')); | |
// Extract the full, raw response which we received on our socket | |
$socket = socket_accept($socket) or die("Error accepting connection on socket: " . socket_strerror(socket_last_error())); | |
$bytes = socket_recv($socket, $buf, 4096, MSG_DONTWAIT) or die("Error reading from socket: " . socket_strerror(socket_last_error())); | |
// Print it so we can see what Guzzle sent in its request | |
print "Read $bytes bytes from socket.\n"; | |
print "Full response:\n\nBEGIN\n###\n$buf\n###\nEND\n\n\n"; | |
// Show the Guzzle Request object for the exact same command, | |
// cast to a string (the way Guzzle shows the full HTTP message) | |
$command = $client->getCommand('MyCommand', array('my_post_param' => 'my_value')); | |
$request = $command->prepare(); | |
print "What Guzzle says it's going to send for this request:\n\nBEGIN\n###\n$request\n###\nEND\n\n\n"; | |
print "Done.\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment