Tag Archives: Computer networking

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;

Diving into Node.js – Very First App

What do I have till now?

After Node.js is istalled, described in my previous post, I can simply run this command:

stoimenpopov:~# node server.js

and this will start the server with the code within server.js. But what’s the code of server.js?

Following the instructions of Node’s homepage and most of the tutorials I’ve found, I can simply copy/paste the code from the first lines of Node’s page:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

There are several things I find interesting in this code, making it different from JavaScript as we know it. First of all what is

var http = require(‘http’)

and why I need it? What is the purpouse of 8124 and 127.0.0.1?

Node is built in modules and to use one of them you must first include it with require. Just like the example above with require(‘http’). In the same manner you can include every module of Node.

What are the Node’s modules are pretty well described in the API page. Well I’d like to say that the API page is quite insufficient. That is very bad, cause most of the code you’ll need developing a node.js applicatoin isn’t described/explained there. Continue reading Diving into Node.js – Very First App

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);

Read Remote File Content-Type with Zend_Http_Client

Check an Image on a Remote Server

This is a common task. You’d like to know whether the image on the remote server exists. Zend Framework gives the answer of this question and in particular this can be completed with Zend_Http_Client.

Content-Type and Content-Length

The little problem is that checking for content-type is not always correct, because it will return an image content type even when the image does not exists, so it’s better to check for content-length.

Code

The simple way to check this is like that:

$client = new Zend_Http_Client('http://remote-machine/image.jpg');
$response = $client->request('head');
if ( NULL == $response->getHeader('Content-Length') )
    // do whatever if the image does not exists.

In fact here, in this example I don’t check for GET request, because is way to slow than the HEAD method, but you should be aware of incorrect responses when using HEAD. In fact if you request the HEAD of a FLV video it will be returned text content-type, while using GET everything’s working fine but slow though.