The usual communication on the Internet consists of a request (Request) to a web server via the HTTP protocol and an answer (response) from the Web server on the same path. The requesting program is called so, HTTP clients. The best known are clearly the browser. But even their own programs that communicate with will fall into this category.
Now you have to reinvent the wheel again. So I would imagine in a small series a couple of solutions to abstract the details of the HTTP protocol. The task is the same for everyone:
- Accessing the Google home page
- Starting a Search
- Showing results left no further description
First up is the package HTTP_Request2 from the Pear repository. HTTP_Request2 supports only the pure HTTP communication, that you will have access to the header with its metadata and the body normally sent to the HTML. The decomposition of the HTML's (parsing) is not included. This task is accomplished by SourceForge project PHP Simple HTML DOM parser .
Enough words, here is the sample code:
A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <Php ; require_once 'HTTP/Request2.php'; ; require_once '/ includes / simple_html_dom.php..'; new simple_html_dom ( ) ; $ Html = new simple_html_dom (); 'http://www.google.de' ; $ Url = 'http://www.google.de'; new HTTP_Request2 ( $url , HTTP_Request2 :: METHOD_GET ) ; $ Request = new HTTP_Request2 ($ url, HTTP_Request2 :: METHOD_GET); try { $request -> send ( ) ; $ Response = $ request -> send (); 200 == $response -> getStatus ( ) ) { if ($ response == 200 -> getStatus ()) { load_file ( $url ) ; $ Html -> load_file ($ url); $html -> find ( 'form' ) ; $ Forms = $ html -> find ('form'); $forms [ 0 ] ; $ Form = $ forms [0]; $form -> getAttribute ( 'action' ) ; $ Action = $ this -> getAttribute ('action'); clear ( ) ; $ Html -> clear (); new HTTP_Request2 ( $url . $action , HTTP_Request2 :: METHOD_GET ) ; $ Request = new HTTP_Request2 ($ url $ action, HTTP_Request2 :: METHOD_GET.); $request -> getUrl ( ) -> setQueryVariable ( 'q' , 'http client examples' ) ; $ Url = $ request -> getUrl () -> setQueryVariable ('q', 'http client examples'); load ( $request -> send ( ) -> getBody ( ) ) ; $ Html -> load ($ request -> send () -> getBody ()); $html -> find ( 'a.l' ) as $link ) { foreach ($ html -> find ('a.l') as $ link) { , "<br />" ; echo $ link, "<br />"; } clear ( ) ; $ Html -> fair (); { Else {} . $response -> getStatus ( ) . ' echo "Unexpected HTTP status: '$ response -> getStatus ()'.. '. getReasonPhrase ( ) ; $ Response -> getReasonPhrase (); } HTTP_Request2_Exception $e ) { } Catch (HTTP_Request2_Exception $ e) { . $e -> getMessage ( ) ; echo 'Error:' $ e -> getMessage (). } ?> |
Lines 7, 8 and 10 go to the Google home page. The success of this action (HTTP status 200) is controlled in line 11. Assuming that the page exists, I'll get myself in line 13, the HTML and looking for it after the first form. Important for further communication, the action (action attribute) is defined for this form.
The action URL + + is the query that in line 21 is added as a parameter to the request revealed the new request. Simple HTML DOM parser, the class can break down not only the HTML from its own inquiry, but also use a string as the source (line 23).
That what excites me most about Simple HTML DOM parser, is access to the HTML elements on the syntax of CSS selectors. Thus, with al accessing any links to the class l.
After that you should still clean up a bit (line 29) - done.
Conclusion:
The team from HTTP_Request2 Pear and PHP Simple HTML DOM Parser is a powerful HTTP client for the scripting language PHP. Precondition is the only one existing Pear environment I would recommend anyway but any PHP programmer.
Outlook:
Part 2 will deal with the Zend Framework. Articles with solutions for Java and Groovy are planned. What HTTP client do you use for your functional tests, or for remote control of Web applications?




October 19th, 2009 at 8:11 am
[...] HTTP Client (Part 2) - + Zend_Http Zend_Dom Web Comments Make Series: Part 1 - Pear [...]
October 21st, 2009 at 7:20 am
[...] Clients (Part 3) - HttpUnit Web Comments Make Series: Part 1 - Part 2 Pear - [...]
November 2nd, 2009 at 12:11 pm
[...] Clients (Part 4) - the way Groovy Web Comments Make Series: Part 1 - Part 2 Pear - Zend Part 3 - Java Part 4 - [...]
December 4th, 2009 at 4:17 pm
Thank you first for the script!
Works great so far but I'll have problems with the transfer of several variables, you help me?
I tried it this way:
$ Url = $ request-> getUrl () -> setQueryVariable ('q', 'http client examples');
replaced by:
$ Params = array ("q" => "'httpclient examples", "num" => "100");
$ Url = $ request-> getUrl () -> setQueryVariables ($ params);
So I tried to Google to get results not just 10 but 100 ...
Can you tell me what I am doing wrong here?
December 6th, 2009 at 12:15 pm
You have an apostrophe too much on "'httpclient examples". With 'httpclient examples' it should work.