Symfony2 Guzzle basic usage with callback

Featured Image © mrjamin@sxc.hu

In previous posts I wrote about Guzzle php web client. Let me continue to show more about that.

Today I am going to make few different request types with Guzzle to https url with self-signed SSL certificate implementing several request types.

Assuming that you read my previous posts, let’s jump straight into the code:

// somecontroller.php
//...
$client = new \Guzzle\Service\Client();
        try {
            switch ($method) {
                case 'GET':
                    $req = $client->get($url, $headers);
                    $req->setOnComplete(function(\Guzzle\Http\Message\RequestInterface $req, \Guzzle\Http\Message\Response $resp, array $default) {
 
                                $resp_content = $resp->getBody(true);
                                $resp_headers = $resp->getRawHeaders();
 
                                //do something with this
                            });
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
                    $req->send();
                    break;
 
                case 'POST':
 
                    $req = $client->post($url, $headers, $body);
                    $req->setOnComplete(function(\Guzzle\Http\Message\RequestInterface $req, \Guzzle\Http\Message\Response $resp, array $default) {
 
                                $resp_content = $resp->getBody();
                                $resp_headers = $resp->getRawHeaders();
 
                                //do something with this
                            });
 
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
                    $req->send();
                    break;
 
                case 'PUT':
                    $req = $client->put($url, null, $body);
                    $req->setOnComplete(function(\Guzzle\Http\Message\RequestInterface $req, \Guzzle\Http\Message\Response $resp, array $default) {
                                $resp_content = $resp->getBody();
                                $resp_headers = $resp->getRawHeaders();
 
                                //do something with this
                            });
 
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
                    $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
                    $req->send();
                    break;
 
                default:
                    $ret = 'Unknown method';
                    break;
            }
        } catch (\Exception $e) {
            echo 'ERROR' . $e->getMessage();
        }
 
//...

Hope that helps to show how to make requests with Guzzle web client and get data from inside callback function.

Cheers.


1 comment

  1. Darko,

    Thanks for blogging about Guzzle!

    If you are setting cURL options on every request, you can automate this by passing some special curl parameters in the constructor of the client:

    $client = new Client(‘http://example.com’, array(
    ‘curl.CURLOPT_SSL_VERIFYHOST’ => false,
    ‘curl.CURLOPT_SSL_VERIFYPEER’ => false
    ));

    Using the above configuration will cause every request created by the client to have the associated cURL options.

    Another note that might interest you: Guzzle 2.0 has been released. Among many major updates, Guzzle now uses the Symfony2 event dispatcher and validator components: http://guzzlephp.org/guide/upgrade/1.0-2.0.html

    -Michael

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.