While I was in the first part in my little series on HTTP clients have employed with Pear, I will now in the 2nd Part of my attention on the Zend Framework addressed.
Here, too, need 2 packages to complete the task (see Part 1 ). The package Zend_Http is like the Pear package Http_Request2 to pure HTTP communication suitable.
With Zend_Dom can disassemble the received HTML.
One way to solve the task might be:
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 38 39 40 41 42 43 44 45 46 47 | <Php ; require_once 'Zend / Http / Client.php'; ; require_once 'Zend / Http / response.php'; ; require_once 'Zend / Dom / query.php'; 'http://www.google.de' ; $ Url = 'http://www.google.de'; new Zend_Http_Client ( $url , array ( $ Client = new Zend_Http_Client ($ url, array ( 0 , 'Maxredirects' => 0, 30 ) ) ; 'Timeout' => 30)); try { $client -> request ( 'GET' ) ; $ Response = $ client -> request ('GET'); 200 == $response -> getStatus ( ) ) { if ($ response == 200 -> getStatus ()) { new Zend_Dom_Query ( $response -> getBody ( ) ) ; $ Dom = new Zend_Dom_Query ($ response -> getBody ()); $dom -> query ( 'form' ) ; $ Forms = $ dom -> query ('form'); $forms -> current ( ) ; $ Form = $ forms -> current (); $form -> getAttribute ( 'action' ) ; $ Action = $ this -> getAttribute ('action'); new Zend_Http_Client ( $url . $action , array ( $ Client = new Zend_Http_Client ($ url. $ Action, array ( 0 , 'Maxredirects' => 0, 30 ) ) ; 'Timeout' => 30)); setParameterGet ( 'q' , 'http client examples' ) ; $ Client -> setParameterGet ('q', 'http client examples'); $client -> request ( 'GET' ) ; $ Response = $ client -> request ('GET'); new Zend_Dom_Query ( $response -> getBody ( ) ) ; $ Dom = new Zend_Dom_Query ($ response -> getBody ()); $dom -> query ( 'a.l' ) as $link ) { foreach ($ dom -> query ('a.l') as $ link) { $link -> hasAttribute ( 'href' ) ) { if ($ link -> hasAttribute ('href')) { $link -> getAttribute ( 'href' ) ; $ Href = $ link -> getAttribute ('href'); $link -> textContent ; $ Link = $ link text - text content>; $href \" > $linkText </a><br />" ; echo "<a href= \" $href \"> $ link text </ a> <br />"; } } { Else {} . $response -> getStatus ( ) . ' echo "Unexpected HTTP status: '$ response -> getStatus ()'.. '. responseCodeAsText ( ) ; $ Response -> responseCodeAsText (); } Zend_Http_Exception $e ) { } Catch (Zend_Http_Exception $ e) { . $e -> getMessage ( ) ; echo 'Error:' $ e -> getMessage (). } ?> |
The code speaks for itself, so I will not explain it at this point as much detail as in the first part.
The difference is in the parsing of HTML's. The query () method (lines 18 and 31) provides that is not an array, but rather an object of class Zend_Dom_Query_Result back. Because this class, the two interface Countable and Iterator implemented, the results of the inquiry with a foreach loop to go through. Every single match is an object of class DOMElement shown.
Conclusion:
Zend also offers a good Zend_Dom Zend_Http and HTTP client. If you use other classes already in his project from the Zend Framework, this approach is recommended.
Outlook:
In Part 3 I leave the PHP world and imagine with HttpUnit a very comfortable and feature-rich HTTP client for Java.



