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.
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);
// 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);
// 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;
// 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;
Here’s the tricky part. As we know from the exec() man page this function executes the given command. As described there, the exact declaration of this method is:
string exec( string $command[,array&$output[, int &$return_var]])
However sometimes that is not so true. Let’s see an example with the popular FFMPEG program. In one hand when you try to start a video converting program with PHP you can simply call exec with the FFMPEG command as the first parameter. The output is OK and you can dump it as in the example here.
In some cases you can call FFMPEG only with the -i option to get some info about the input file. Such info can be the size of the image, the bitrate of the source file or the length of the movie. Executing this command:
in the terminal you’ll see the output you wish. However if you trying to execute the command via exec() the output remains empty?
Why the Output Parameter is Empty?
Looking at the terminal screen you can’t see the difference, but actually in the second case, when you execute FFMPEG only with the -i option, the output is redirected to the standard error output. That’s why you can’t see it in the exec() second parameter. However there is a solution.
Sometimes PHP's exec() doesn't return the command output. You've to be sure that the command output is not redirected to the standard error stream.
Solution and Conclusion
The solution is quite simple. You’ve to redirect the output of the FFMPEG command to the standard output.
This showcase with the FFMPEG is important not only because now you can call FFMPEG’s command without problems, but because you now know that exec() can’t read the standard error output. Thus you’ve to be careful and always redirect to the standard output.
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 existsif(0===(int)$response->getHeader('Content-Length')){echo'the file doesn\'t exits';}
// 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.
You’ve definitely seen the “share a link” screen in Facebook. When you paste a link into the box (fig. 1) and press the “Attach” button you’ll get the prompted cite parsed with a title, description and possibly thumb (fig. 2). This functionality is well known in Facebook, but it appears to be well known also in various social services. In fact Linkedin, Reddit, Dzone‘s bookmarklet use it.
fig. 1 - Facebook Attach a Link Prompt Screen
Fist thing to notice is that this information, prompted by Facebook, is the same as the meta tag information. However there is a slight difference.
fig. 2 - Facebook Attached Link Screen
Facebook prefers for the thumb the image set into the <meta property=”og:image” … />. In the case above this tag appears to be:
First of all what do we have? There is a vector shape, which may represent a map, which we’d like to convert into a GEO map. In other words there is a SVG file containing the source shape, that you’d like to convert in geoJSON or whatever collection of geo points. This is not trivial, of course, first of all because there’s no an algorithm or automation that can do this, and because everybody knows that the resulting map will be only approached, but will never be so accurate as the vector shape. This is because in a vector shape you may contain Bézier Curves, which I’ll show a little bit later in this post, that are difficult to represent in geo coordinates.
So the first task will be to find an approaching algorithm. However there are two things that are optimistic:
You can’t effectively represent Bézier curves in geo coordinates, but even if you could do it there’s no practical need, because the collection of geo coordinates will be huge and this will slow down you’re application. Remember that geoJSON is yet again possibly used by your browser and the amount of geo points will be proportionally slowing down the app.
As you may know Google’s visualization map is representing the World’s countries again with quite approached maps. Take a look at the following image – the country borders are quite sharpened: