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 -> clear ();
{ 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 me in row 13, the HTML and look 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, the 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?
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: Calling the Google home page Start a search results display the links with no further description First up is the Pear HTTP_Request2 package from the 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 example code: 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 me in row 13, the HTML and look 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, the access to the HTML elements on the syntax of CSS selectors. Thus, access to all links with al l the class. 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?
gklinkmann written by \ \ tags: browser , php , tutorial , web