Yii 1.1: ehttpclient

Http Client Extension for Yii
29 followers

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.

I have created a GitHub repository for those willing to contribute on any of the extensions I created. Please, check the link at the bottom of this wiki.

Requirements

Working from Yii v1.1.5 till v1.1.9

TODO

Add streaming support

Usage

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();

ChangeLog

  • version 1.0.2 Fixed really silly reference bug
  • version 1.0.1 Added streaming support
  • version 1.0.0 Initial public release

Resources

Total 15 comments

#18499 report it
kulikov-d-n at 2014/11/06 04:44pm
Segmentation Fault

For reference to all who can encounter Segmentation Fault using this extension: it may be caused by very long URL. You should avoid using extremely long URLs, but if it is still needed you can try workaround in description of this issue ZF-10151: Improved notification in Http.php validateQuery() over preg_match crash caused by PHP backtracking bug.

This issue on GitHub.

#17367 report it
Luciano Santana at 2014/05/29 09:07am
@nima_naraghi

To fix that you just need to add an extra $encodeValue parameter to the function definition.

public static function fromString($cookieStr, $ref_uri = null, $encodeValue)
#17139 report it
nima_naraghi at 2014/05/07 03:10am
Missing encodeValue arguments in EHttpCookie.php

$client->setCookieJar() makes error. Please Fix it and let me know if it effects on other part of extension.

#14430 report it
zerolimit at 2013/08/11 10:09am
Error Handling

Hey, good job for the extension. It work's perfectly. But I ran into a problem. If the website didn't exist, I get a PHP error. How I should handle this? I tried to use "@$client->request()" but I still get to see the error.

PHP Error code: Unable to Connect to tcp://exampleerror.com:80. Error #0: php_network_getaddresses: getaddrinfo failed: No such host is known.

#11327 report it
AustinGeek at 2013/01/06 11:15am
minor problem installing

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

#8372 report it
webservice at 2012/05/29 10:10am
@tonydspaniard

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 :(

#8350 report it
Antonio Ramirez at 2012/05/28 08:12am
@webservice

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...

#8336 report it
webservice at 2012/05/27 04:58am
Dependency

Is there any Dependency in the php version because this ext it doesn't work in my server while locally works fine.

#4021 report it
Antonio Ramirez at 2011/05/29 10:38am
@joblo

Many thanks for your feedback and your extension.

#4016 report it
Joblo at 2011/05/27 12:17pm
ETouchHttpClient

I have published this component as part of the extension runactions

#3924 report it
Joblo at 2011/05/21 08:37pm
EHttpTouchClient

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)

class EHttpTouchClient extends EHttpClient
{
    /**
     * Send the HTTP request without reading the result
     *
     * @param string $method
     * @return EHttpResponse
     * @throws EHttpClientException
     */
    public function request($method = null)
    {
        if (! $this->uri instanceof EUriHttp) 
 
                   ...
 
        $this->last_request = $this->adapter->write($this->method,
            $uri, $this->config['httpversion'], $headers, $body);
 
        return true;
 
                //$response = $this->adapter->read();
 
    }

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.

#2467 report it
Antonio Ramirez at 2011/01/06 12:06pm
@ddreggors

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.

#2454 report it
David Dreggors at 2011/01/04 12:22pm
A little more info please.

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

#2434 report it
Antonio Ramirez at 2010/12/30 03:15pm
@pcs2112

"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)."

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

#2429 report it
pcs2112 at 2010/12/29 09:49pm
Question

Is this the same code as Zend_Http_Client? I see all the dependencies classes are similar. Good job btw.

Leave a comment

Please to leave your comment.

Create extension