Yii Framework Forum: EHttpClient Extension - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

EHttpClient Extension An Http Client Extension for Yii Rate Topic: -----

#1 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 27 December 2010 - 10:48 AM

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

http://www.yiiframew...on/ehttpclient/


Bug Reports, Feedback highly appreciated.


Best!
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#2 User is offline   David Dreggors 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 09-November 10
  • Location:Orlando, FL

Posted 04 January 2011 - 01:54 PM

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?
The requirements said Windows XP or better, so I installed Linux!
0

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 04 January 2011 - 02:52 PM

View PostDavid Dreggors, on 04 January 2011 - 01:54 PM, said:

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).
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 04 January 2011 - 03:11 PM

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

#5 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 04 January 2011 - 04:08 PM

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.zen...t.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.zen...p.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.
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#6 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 04 January 2011 - 04:45 PM

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

#7 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 04 January 2011 - 05:01 PM

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. :)
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#8 User is offline   putera 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 17-October 11

Posted 18 October 2011 - 10:37 AM

View PostAntonio Ramirez, on 27 December 2010 - 10:48 AM, said:

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



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`
0

#9 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 18 October 2011 - 11:57 AM

Wow... great one, thanks for sharing *putera*
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#10 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 06 January 2013 - 01:17 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users