EHttpClient Extension

Zend Http Client library modified for easy inclusion with the Yii framework ( until Yii creates its own :) )

http://www.yiiframework.com/extension/ehttpclient/

Bug Reports, Feedback highly appreciated.

Best!

Antonio, I was looking at your ehttpclient extension and I am a bit confused on a couple of things though…

  1. Where do you place that code example (controller, view, or a mixture of both)?

  2. Is there any example that shows how to go from raw XML to something that more resembles an rss feed?

I am new to Yii and have never used Zen at all. The twitter example on the extension’s page works if I place it all in a view but I get raw XML output to the screen.

Do I have to then take that xml object ( "$response->getBody()" ) and pass it to an external XML parsing function like simpleXML?

Can you give an example if at all possible?

Hi David,

The code example is purposely created to be placed in a view as an example. The objective with the example is that, in fact, works to GET a Page. This extension allows programmers to POST, GET to external servers from PHP, receive a response and do whatever with it.

The extension has been ported from Zend, so you can read its documentation to use this one. It should work as the one in Zend. I still have to write some articles on how to use it but I am so busy these days, that is nearly impossible to do things further than just posting in this forum sometimes.

The example makes a request to the TWITTER API and returns its results. The results are XML so you obviously need a XMl parser (SimpleXML will perfectly do).

Does it support max response size (couldn’t find it while taking a quick look)?

Hi there Y!!,

To be honest, I have never used that option in any of my HTTP requests, and as far as I know for the work I did on these classes, these are the config options for the client:

http://framework.zend.com/manual/en/zend.http.client.html#zend.http.client.configuration

Every adapter has its own options() array for its configuration and again, I have never used that option. If the client does not suit your needs, you can still use EHttpResponse like explained here:

http://framework.zend.com/manual/en/zend.http.response.html

Could you provide me an example of that option Y!!, I may be implement it if the extension isn’t capable of setting it.

Ahh sorry I mean if there’s such a feature at all. I know that curl doesn’t support it (at least not the php extension, the command line version does), so the curl adapter won’t have this feature. Now I’ve took a look at the socket adapter documentation and there’s no such option. :( I guess I will write my own http client then.

Maybe you can build one to adapt to this client. Would it be great. At the moment, it supports socket and curl. Check its interfaces, they are easy to implement. :)

Hi thanks for the extension.

I used it for a simple functional/integration test, and i found this extension is useful.

Here example of my functional test, as alternative to testing with WebTestCase, it doesn’t need Selenium, and it runs faster.




// protected/tests/functional/PagesControllerTest.php

<?php


Yii::import('ext.httpclient.*');

Yii::import('ext.httpclient.adapter.*');


class PagesControllerTest extends PHPUnit_Framework_TestCase {


  public function testHome() {

	$client = new EHttpClient(TEST_BASE_URL.'/pages/home');

	$this->assertTrue($client->request()->isSuccessful());

  }


  public function testContact() {

	$client = new EHttpClient(TEST_BASE_URL.'/pages/contact');

	$this->assertTrue($client->request()->isSuccessful());

  }


  public function testError() {

	$client = new EHttpClient(TEST_BASE_URL.'/pages/error-not-found');

	$this->assertTrue($client->request()->isError());

  }


  public function testLoginLogout() {

	$client = new EHttpClient(TEST_BASE_URL.'/site/login');

	$client->setCookieJar();

	$this->assertTrue($client->request()->isSuccessful());

	$this->assertRegExp('/Login/', $client->request('POST')->getBody());


	$client->resetParameters();

	$client->setParameterPost(array(

  	'LoginForm[username]' => '',

  	'LoginForm[password]' => ''

	));

	$this->assertRegExp('/Username cannot be blank./', $client->request('POST')->getBody());

	$this->assertRegExp('/Password cannot be blank./', $client->request('POST')->getBody());


	$client->resetParameters();

	$client->setParameterPost(array(

  	'LoginForm[username]' => 'wronguser',

  	'LoginForm[password]' => 'wrongpass'

	));

	$this->assertRegExp('/Incorrect username or password./', $client->request('POST')->getBody());


	$client->resetParameters();

	$client->setParameterPost(array(

  	'LoginForm[username]' => 'demo',

  	'LoginForm[password]' => 'demo'

	));

	$this->assertNotRegExp('/Username cannot be blank./', $client->request('POST')->getBody());

	$this->assertNotRegExp('/Password cannot be blank./', $client->request('POST')->getBody());


	$client->setUri(TEST_BASE_URL);

	$this->assertTrue($client->request()->isSuccessful());

	$this->assertRegExp('/Logout \(demo\)/', $client->request('POST')->getBody());


	$client->setUri(TEST_BASE_URL.'/site/logout');

	$this->assertTrue($client->request()->isSuccessful());


	$client->setUri(TEST_BASE_URL);

	$this->assertNotRegExp('/Logout \(demo\)/', $client->request('POST')->getBody());

  }


}



run it with phpunit -c protected/tests/ protected/tests/functional/PagesControllerTest.php

1 Like

Wow… great one, thanks for sharing putera

Hi Antonio,

Great Extension! Thank you!

I am having some trouble and think I am missing something simple…

Here is what I would do from command line in curl:





I have a file named "auth" in a working directory


$ cat auth

{

  "auth": {

      "username": "myusername",

      "password": "mypassword"

  }

}


I issue the curl command as follows:


$ curl -b cookies -c cookies -X POST --data-binary @auth 'http://sand.api.appnexus.com/auth'

{

  "response": {

    "status": "OK",

    "token": "7nod933p0o9pv6o2mg10i5g5u0"

  }

}


After that my cookie holds the token so I can continue to issue commands to the server and return JSON data.



If I want to create two simple routines "Authorize" (POST) and "GetSomeData" (GET) in a view I started with your example like this:





// Auth


$client = new EHttpClient('http://api.appnexus.com/auth', array(

    'maxredirects' => 0,

    'timeout'      => 30));


// $auth = $client->setAuth('username', 'password', EHttpClient::AUTH_BASIC);

// $client->setParameterPost(array('auth'=>$auth));

 

$response = $client->request('POST');

 

if($response->isSuccessful())

    echo '<pre>' . htmlentities($response->getBody()) .'</pre>';

else

    echo $response->getRawBody();


$client = new EHttpClient('http://api.appnexus.com/category', array(

    'maxredirects' => 0,

    'timeout'      => 30));



I don’t think I have the $auth correct as I get a response of

{"response":{"error_id":"SYSTEM","error":"Username or password not set in request"…}

when the line "$auth = …" line is commented out

If I try to use it, I get

{"response":{"error_id":"SYNTAX","error":"JSON decode failed",…}

I am close…

I think my JSON decode error is the remote system not liking the format sent to it.

I am hoping someone can give me an idea as to how best to send my username/password and either use cookies or the token returned to do a GET for the data after authorizing.

Thanks again in advance for any assistance.

Hi, it seem to be have problem with DELETE method. When I try to use DELETE method to request, and set parameter with post ($client->setParameterPost($params)), the client response an 504 error status ?

I am facing issue to call API hosted on another server which HTTPS. SO I am getting error like "Exception: Unable to Connect to ssl:domainname".

I have seen that cURL default not support HTTPS call and there is also some work around for this in HTTPCLIENT library in ZEND.

What should I do here in Yii to solve the issue?

ANy help is appreciated.

Sample code is as below

$postResponse = $this->doPost

            (


            APIURL . &quot;api/UserBasicInfoWithoutLogin&quot;, array('timeout' =&gt; LONG_OPERATION_TIMEOUT), array(


        'app_id' =&gt; &#036;app_id,


            )


    );

I changed it to something like below after reading zend forum but no clue in this also

$postResponse = $this->doPost

            (


            APIURL . &quot;api/UserBasicInfoWithoutLogin&quot;, 


            array(


                'timeout' =&gt; LONG_OPERATION_TIMEOUT,


                'adapter' =&gt; EHttpClientAdapterSocket,


                'ssltransport' =&gt; 'ssl'


            ),


            array(


                'app_id' =&gt; &#036;app_id,


            )


    );

Ill rewrite this extension at this week - it not work properly.

Also I want use dnsmasq instead isc-dhcp - it know work as name server for zone local.

have you got the solution? i have the same issue…