Tag Archives: Network protocols

POST with Zend_Http_Client

CURL and Zend_Http

It’s a well know fact that you can preform HTTP requests with CURL. Zend Framework does the same job with Zend_Http. Especially Zend_Http_Client can be used to “replace” the usual client – the browser, and to perform some basic requests.

HTTP requests can be performed with Zend_Http_Client
Zend_Http_Client is mostly used to perform GET requests, but it can be also very helpful for POST HTTP requests.

I’ve seen mostly GET requests, although Zend_Http_Client can perform various requests such as POST as well.

// new HTTP request to some HTTP address
$httpClient = new Zend_Http_Client('http://www.example.com/');
// GET the response
$response = $httpClient->request(Zend_Http_Client::GET);

Here’s a little snippet showing how to POST some data to a server.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);

Note that the request method returns a response. Thus if you are simulating a form submit action you can “redirect” to the desired page just like the form.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
echo $response->location;

Send Authenticated POST Request with Zend_Http_Client


There is an easy way to send requests with Zend_Http_Client either in GET and POST. Actually you can request not only with GET and POST methods, but with any other http request method.

$httpClient = new Zend_Http_Client('http://....');
// request via post
$response = $httpClient->request(Zend_Http_Client::POST);
// or get
$response = $httpClient->request(Zend_Http_Client::GET);

However the interesting and useful thing is that you can perform authenticated requests:

$httpClient = new Zend_Http_Client('http://username:password@example.com');
// or define it later
$httpClient = new Zend_Http_Client('http://example.com');
$httpClient->setAuth('username', 'password');

That’s an opportunity to send/receive authenticated POST requests.

$httpClient = new Zend_Http_Client('http://username:password@example.com');
// now post
$httpClient->request(Zend_Http_Client::POST);

Zend_Http_Client and Case Sensitivity

Recently I posted about Zend_Http_Client and the ability to check a request HEAD or GET. It is, as everything in Zend Framework, extremely easy to adopt and use, but today I experienced some “strnage” problems!

As I wrote some lines of code,

<?php
....
$client = new Zend_Http_Client('uri_path');
$request = $client->request('head');
...
?>

so far so good, everything worked just perfect until I deployed the “working” code on the production server. Than odd enough some requests just crashed. Not all – just some of them.

Another strange thing was that no exception was thrown.

Maybe using more my intuition instead of my brain I just tried to request the head using capital letter – HEAD. And as everything was strange enough, yet again this solution happened to be working! That solved my problem.

<?php
....
$client = new Zend_Http_Client('uri_path');
$request = $client->request('HEAD');
...
?>