I wanted to have a proper Http Client for my Yii Applications. Even though there are a couple of good classes around, I wanted a good one to extend from and also that has an already proven efficiency. So I got into Zend Http Client library (I really do not like to reinvent the wheel -do not worry Zend guys, I have already included the license in it). Hope Yii comes in near future with an HTTP Client by its own, I think is a great thing to have to develop greater things (mash-up applications, social network connectors, universal password validations, Xml-Rpc Servers-Clients, and so on...)
I have worked through the classes and checked that all that is required is within the package. So, with this library you do not need to worry about the inclusion of the Zend Library on the vendors folder.
This version is beta and even though I have tested most of its functionality, I would love your feedback and tests to improve it and make it worth to in have our must have libraries.
Working from Yii v1.1.5 till v1.1.9
Add streaming support
Unpack compressed files and place them on your protected/extensions directory.
You can refer to the Zend_Http_Client documentation (remember to exchange the names from Zend to those in the extension).
Here two easy samples (Send me your tests, I would love to see more in action)
Example 1 - Getting my twitter statuses
Yii::import('ext.httpclient.*'); // get my twitter status $client = new EHttpClient('http://twitter.com/statuses/user_timeline/tonydspaniard.xml', array( 'maxredirects' => 0, 'timeout' => 30)); $response = $client->request(); if($response->isSuccessful()) echo '<pre>' . htmlentities($response->getBody()) .'</pre>'; else echo $response->getRawBody();
Example 2 - Performing a Full Page Google Search Now, using EHttpClientAdapterCurl
Yii::import('ext.httpclient.*'); $client = new EHttpClient('http://www.google.es/search', array( 'maxredirects' => 3, 'timeout' => 30, 'adapter' => 'EHttpClientAdapterCurl')); $client->setParameterGet(array('hl'=>'es', 'q'=>'manolo')); $response = $client->request(); if($response->isSuccessful()) echo $response->getBody(); else $response->getRawBody();
Example 3 - Downloading using stream
Yii::import('ext.httpclient.*'); $uri = EUri::factory('http://www.ip2nation.com/ip2nation/Download/'); $temp_dir = realpath(sys_get_temp_dir()). DIRECTORY_SEPARATOR; $temp_file = tempnam($temp_dir, 'TEST'); $config = array('adapter'=>'EHttpClientAdapterCurl', 'timeout'=>'60'); $client = new EHttpClient($uri, $config); $client->setStream(); $response = $client->request('GET'); if($response->isSuccessful()){ // copy stream to temporary file... copy( $response->getStreamName(), $temp_dir . $temp_file ); // other work with temporary file (ie decompress, decrypt, etc...) } else $response->getRawBody();
Total 11 comments
Hi Antonio, This is a great extension, THANK YOU!
I am new to Yii but have seen many examples of your work and you have shared alot with this community!
I ran into a simple problem with the extension Yii::import('ext.httpclient.*'); is not accurate. It installed in extensions/EHttpClient/ so I had to make a simple edit. From what I can see this extension may have started life as "httpclient" and the examples were never adjusted. Hope this helps someone save 5 minutes. I am a novice but I like to contribute where I can.
Thanks again! Bryan
Locally I run windows 7 with xampp and online I have a vps on hostdime. I use ehttpclient with runactions to send emails through phpmailer but online doesn't work. The phpmailer online works but inside the ehttpclient & runactions it doesn't work. There is no error that gives me some hint :(
What is the error you have?
What is your development machine and your production server? Is it Windows on your local and Linux on server side? If that is so, make sure there is no naming conflicts...
Is there any Dependency in the php version because this ext it doesn't work in my server while locally works fine.
Many thanks for your feedback and your extension.
I have published this component as part of the extension runactions
Maybe useful for others too:
I often need to 'touch' a url, that means to start processes at a webserver (importing data, flush mail spooler from swiftMailer, ...) without waiting for a response.
For example I use this when sending large mails 'asynchronous' by storing in the swiftMailer FileSpool and afterwards 'touch' the controller action to flush the queue and send the message. So the user don't have to wait until the mail is sent.
All you have to do is to override the request method of EHttpClient and return before reading the response (line 798)
Would be nice to add a property 'touchOnly' to the EHttpClient that returns before reading, so I don't need an extra class for this feature.
Besides ... many thanks for this great extension.
The way to do it is by using an XML parser. The extension allows to create an HTTP client and make requests to external servers from PHP.
SimpleXML would do to parse an XML file.
This is neat but when I add the twitter code above to a Yii view I get raw xml on my page. I have read through the Zend Http Client links you posted and I see where you can POST XML but nowhere that I have found is there a method to "parse" the xml into an html tree or rss feed style of output.
Is there way to convert that into something that more resembles an rss feed?
Consider these 2 links:
THIS IS WHAT I SEE ON MY PAGE... http://twitter.com/statuses/user_timeline/ddreggors.xml
THIS IS WHAT I WOULD LIKE TO SEE ON MY PAGE... http://twitter.com/statuses/user_timeline/ddreggors.rss
I wanted to use the classes but adapted to Yii's style. It is all same but narrowed-adapted. Still lots to do, but it is a good base for anybody to start with it and change it to suit our needs.
Thanks
Is this the same code as Zend_Http_Client? I see all the dependencies classes are similar. Good job btw.
Leave a comment
Please login to leave your comment.