API (Application Programming Interface)

Using Yii can i create my own API for my website, such as FB connect? , if i want to create my own site MYSITE CONNECT via API, using XML-RPC , is it possible with Yii Framework?

Hi Salman, it is all possible with Yii as what it doesnt have, you can incorporate quite easily from others.

Yii lacks of an HttpClient if that is what you are looking for, but you can do it by getting Zend Http Client into the framework or build your own :)

I have created a modified version of Zend, so it is easier to incorporate to Yii (more like an extension). I didn’t have much time to debug but if you wish to try it, it would be my pleasure to share the code with you.

Cheers

Why only with him? :] Post it as an extension and it will be our pleasure to test it and tell you what is wrong! :}

I would be very interested in this too! :)

Am going to need an API for my Yii powered bugtracker so that people can talk to it from Tortoise tools (SVN/GIT/Hg) using the Buqtracker api.

And so I need xmlrpc or other rest-like interface to Yii.

I have some ideas about this already, but I’d hate to dig myself into the wrong hole. :)

hahaha ok man…

Keep an eye on your inbox and lets try it together… ok?

Maybe create a Webservice?

http://www.yiiframework.com/doc/guide/1.1/en/topics.webservice

Webservice and SOAP does not appeal to me in any way whatsoever.

WSDL ? Gimme a break.

For most applications it’s like shooting sparrows with cannons.

IMO, of course.

And you need to overload your web server with soap.

It’s a pity that that’s the only option shipped with Yii.

I’ll definitely be checking out Antonios alternative. :)

I don’t understand your concerns. Did you read the guide page? You don’t have to deal with WSDL files at all. Set up a webservice, and create a SoapClient object on another server - done. You’ll be able to access the provided service actions just like object methods on the client. I don’t know, how much easier it can get :)

I am not dismissing it (much) :)

I know that it’s easy to set up and use, but I’d prefer to have a very lean (not powered by soap) solution.

As an alternative.

I have two hosts - one where it’s not possible to install the soap component on (no shell access), and another where I’d need to do a local php installation to install it - so I cannot fire it up. Even if I wanted to.

And, maybe it’s overkill as I only want a very basic API. ;)

Uhm - just to make sure (your post wasn’t clear to me on that): You do know that SOAP is only a PHP extension - which is already enabled by default on many PHP installations?

And i still can’t agree to your arguments, that SOAP doesn’t match because it’s “overkill” and you only want a “very basic API”. Why should SOAP be overkill and why should it only be apropriate for complex APIs? A webservice can consist of a single action, and a SoapClient is a very simple object which can be set up with 1 line of PHP. Any other solution that comes to my mind is far more complex to set up.

I checked, and it’s enabled on my own host.

You’re right, as usual. :lol:

However, it’s not enabled on Dreamhost, though.

I’ll dig into performing some tests not using it, because I am determined to demonstrate that it’s simple enough to have an API without SOAP.B)

I have developed several sites for rent-a-car systems (www.bravorentacar.com, www.torrenovarentacar.com) using PHP’s SOAP client. Very fast jacmoe, Mike is right.

Nevertheless, I still think that a CHttpClient class could be a great feature to have into the collection of Yii classes as at this will allow us ‘mimic’ browser features and communicate with Api services in a different way that just SOAP, dont you agree? Also, it will easy the tasks for programmers to do a server-to-server communications. IMHO of course.

It’s not very hard to write a nice OOP wrapper class around the curl_* PHP functions. I’ve written a very basic client class on a closed source project once. It was pretty straightforward - if you start at the end and think about the preferred interface of such a client first. Maybe something like:




$browser=new HttpClient;

$browser->proxy='http://myproxy.com:8080';

$html=$browser->get('http://www.google.com');


$result=$browser->post('http://example.com/someform.php',array('name'=>'val1'));



And if you extend it from CComponent you get all the Yii goodies (getter/setter, events,…) and can even configure it as application component etc.

EDIT:

Ha, i missed Igor’s Post, he developed something similar:

http://www.yiiframework.com/forum/index.php?/topic/14213-curl-component/page__pid__70388#entry70388

I think what I’m really saying is that I’d hate to rewrite existing apps (which includes desktop apps written in C++/C#) which uses REST or XMLRPC to use SOAP.

I have a feeling that’s what I really mean.

It’s good to hear that webservices powered by SOAP is fast.

However, sometimes … :)

well between Zend framework is not very efficient as Yii, Zend is just like windows for me, and Yii is just like Linux :lol:

I am trying to learn Yii as best i can, i am new in Web Applications, i have experience

for PHP skill and smarty but not upto professional scale

I did some testing and even though I didn’t have much time, I ended up with REST. :)

I grabbed the patch for CUrlManager here:

http://code.google.c…s/detail?id=802

Then I created a CRestUrlManager (the patched up CUrlManager) and put the class name in the urlmanager config.

Then I grabbed this code from the net:


<?php


class RestRequest

{

	protected $url;

	protected $verb;

	protected $requestBody;

	protected $requestLength;

	protected $username;

	protected $password;

	protected $acceptType;

	protected $responseBody;

	protected $responseInfo;


	public function __construct ($url = null, $verb = 'GET', $requestBody = null)

	{

		$this->url				= $url;

		$this->verb				= $verb;

		$this->requestBody		= $requestBody;

		$this->requestLength	= 0;

		$this->username			= null;

		$this->password			= null;

		$this->acceptType		= 'application/json';

		$this->responseBody		= null;

		$this->responseInfo		= null;


		if ($this->requestBody !== null)

		{

			$this->buildPostBody();

		}

	}


	public function flush ()

	{

		$this->requestBody		= null;

		$this->requestLength	= 0;

		$this->verb				= 'GET';

		$this->responseBody		= null;

		$this->responseInfo		= null;

	}


	public function execute ()

	{

		$ch = curl_init();

		$this->setAuth($ch);


		try

		{

			switch (strtoupper($this->verb))

			{

				case 'GET':

					$this->executeGet($ch);

					break;

				case 'POST':

					$this->executePost($ch);

					break;

				case 'PUT':

					$this->executePut($ch);

					break;

				case 'DELETE':

					$this->executeDelete($ch);

					break;

				default:

					throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');

			}

		}

		catch (InvalidArgumentException $e)

		{

			curl_close($ch);

			throw $e;

		}

		catch (Exception $e)

		{

			curl_close($ch);

			throw $e;

		}


	}


	public function buildPostBody ($data = null)

	{

		$data = ($data !== null) ? $data : $this->requestBody;


		if (!is_array($data))

		{

			throw new InvalidArgumentException('Invalid data input for postBody.  Array expected');

		}


		$data = http_build_query($data, '', '&');

		$this->requestBody = $data;

	}


	protected function executeGet ($ch)

	{

		$this->doExecute($ch);

	}


	protected function executePost ($ch)

	{

		if (!is_string($this->requestBody))

		{

			$this->buildPostBody();

		}


		curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);

		curl_setopt($ch, CURLOPT_POST, 1);


		$this->doExecute($ch);

	}


	protected function executePut ($ch)

	{

		if (!is_string($this->requestBody))

		{

			$this->buildPostBody();

		}


		$this->requestLength = strlen($this->requestBody);


		$fh = fopen('php://memory', 'rw');

		fwrite($fh, $this->requestBody);

		rewind($fh);


		curl_setopt($ch, CURLOPT_INFILE, $fh);

		curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);

		curl_setopt($ch, CURLOPT_PUT, true);


		$this->doExecute($ch);


		fclose($fh);

	}


	protected function executeDelete ($ch)

	{

		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');


		$this->doExecute($ch);

	}


	protected function doExecute (&$curlHandle)

	{

		$this->setCurlOpts($curlHandle);

		$this->responseBody = curl_exec($curlHandle);

		$this->responseInfo	= curl_getinfo($curlHandle);


		curl_close($curlHandle);

	}


	protected function setCurlOpts (&$curlHandle)

	{

		curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);

		curl_setopt($curlHandle, CURLOPT_URL, $this->url);

		curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType));

	}


	protected function setAuth (&$curlHandle)

	{

		if ($this->username !== null && $this->password !== null)

		{

			curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

			curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);

		}

	}


	public function getAcceptType ()

	{

		return $this->acceptType;

	}


	public function setAcceptType ($acceptType)

	{

		$this->acceptType = $acceptType;

	}


	public function getPassword ()

	{

		return $this->password;

	}


	public function setPassword ($password)

	{

		$this->password = $password;

	}


	public function getResponseBody ()

	{

		return $this->responseBody;

	}


	public function getResponseInfo ()

	{

		return $this->responseInfo;

	}


	public function getUrl ()

	{

		return $this->url;

	}


	public function setUrl ($url)

	{

		$this->url = $url;

	}


	public function getUsername ()

	{

		return $this->username;

	}


	public function setUsername ($username)

	{

		$this->username = $username;

	}


	public function getVerb ()

	{

		return $this->verb;

	}


	public function setVerb ($verb)

	{

		$this->verb = $verb;

	}

}



Put this in my url manager config array:


        	'resources' => array('post', 'page'),



Modified my controller to output a json array.

And ran this code:


$request = new RestRequest('http://localhost/blog/page/resources', 'GET');

$request->execute();


echo '<pre>' . print_r($request, true) . '</pre>';



And lo and behold… :)

Every thing nicely tucked in a json array.

Now I need to figure out how to figure out when to return json and when to render normally…

Any ideas?

<edit>

Maybe just add a getIsRestRequest or similar function?

</edit>

Of course, if I just decide that I only want the api controller to be serving rest requests, then I don’t have to figure that out.

But, using the patch above, you can have auto-rest in your yii apps. ;)

Of course, no authentication, api keys or the like. Yet.

Credits:

original article at gen-x-design.com

Found out a way to handle the rest requests (sort of):


    	if (Yii::app()->request->getAcceptTypes() == 'application/json') {

        	echo json_encode($page);

    	}else {

        	$page = $this->loadModel($slug);



I am not happy with it, but…

Now on to using the new CUrl extension. :)

well is it possible to make powerful API just like facebook using Yii?

answer should be yes or no :D

if yes, then post some highlight on it

Yes. :)

Highlight will follow. B)

I’ve just started looking into this, but why not?