Tag Archives: http

PHP: Fetch $_GET as String with http_build_query()

PHP is really full of functions for everything! Most of the time when you try to do something with strings, there’s a function that can do it better and faster.

The Route from $_GET to String

The global arrays in PHP contain request parameters. Either GET or POST. As you know if the page address is something like:

http://www.example.com/index.php?a=b&key=value

This means that you pass to the index.php file two parameters – “a” and “key” with their values: “b” and “value”. Now in this case you can dump the $_GET global array somewhere in index.php and you’ll receive something like this.

array(
	"a"   => "b",
	"key" => "value",
);

This is however pseudocode, but in fact $_GET will be very similar to this sample array. Continue reading PHP: Fetch $_GET as String with http_build_query()

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;

Use fopen() to Check File Availability?

Zend Framework and Zend_Http_Client

PHP's fopen() can be used to check remote file existence
PHP's fopen() can be used to check remote file existence

I’ve posted about Zend_Http_Client. Simply there you can ‘make’ your own http client and you can request a remote file. Just to check what’s going on with this file.

// new HTTP request to a file
$httpClient = new Zend_Http_Client('http://www.example.com/myfile.mp4');
 
// get the HEAD of the response and match agains the
// Content-Length. That's because using the Content-Type is slower
$response = $httpClient->request(Zend_Http_Client::HEAD);
 
// if the Content-Length is 0 the file doesn't exists
if (0 === (int)$response->getHeader('Content-Length')) {
	echo 'the file doesn\'t exits';
}

However is there any other way to answer the same question?

fopen()

Yes and no? Perhaps yes, but you should be careful. I’m still not sure it can be used in any case. However here’s the snippet.

if (FALSE === @fopen('http://www.example.com/myfile.mp4', 'r')) {
	echo 'the file doesn\'t exists';
}

fopen() will return FALSE whenever the file doesn’t exists.

In both cases I request a remote file – an MPEG-4 file. Note that fopen()’s first parameter can be a HTTP resource.

Diving into Node.js – A Long Polling Example

Node.js vs. The World

What is typical for most of the web servers is that they listen for requests and respond as quickly as possible on every one of them. In fact the speed of the response is one of the main targets of optimization for developers. Fast servers are what everyone needs. From web developers to website visitors!

In the field of the that battle different web servers have different “weapons” to gain time. While this is useful in most of the cases, when it comes to a chat-like applications and Node.js approaches, the response is not always immediately returned. As I described in my posts until now about Node.js, a simple web server may wait for an event to be emitted, and than return the response. Continue reading Diving into Node.js – A Long Polling Example