views:

1324

answers:

4

I'm building a REST web service client in PHP and at the moment I'm using curl to make requests to the service.

How do I use curl to make authenticated (http basic) requests? Do I have to add the headers myself?

If so I've got some other questions -

  1. Is there a REST library for php?

  2. or is there a wrapper for curl that makes it a bit more rest friendly?

  3. or am I going to have to continue to roll my own?

Thanks.

+9  A: 

You want this:

curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);  

Zend has a REST client and zend_http_client and I'm sure PEAR has some sort of wrapper. But its easy enough to do on your own.

So the entire request might look something like this:

$process = curl_init($host);                                                                         
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));              
curl_setopt($process, CURLOPT_HEADER, 1);                                                                           
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);                                                
curl_setopt($process, CURLOPT_TIMEOUT, 30);                                                                         
curl_setopt($process, CURLOPT_POST, 1);                                                                             
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);                                                            
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);                                                                
$return = curl_exec($process);  
Mr-sk
The Zend REST client is here: http://framework.zend.com/manual/en/zend.rest.client.html
Pekka
Cool thanks Pekka
Mr-sk
@Pekka - your link is broken as you miss http://
Jay Zeng
@Jay, works for me when I click it. Doesn't it for you?
Pekka
@Pekaa - strange, now it works :) anyway, trivial thing.
Jay Zeng
+1  A: 

Yahoo has a tutorial on making calls to their REST services using PHP:

Make Yahoo! Web Service REST Calls with PHP

I have not used it myself, but Yahoo is Yahoo and should guarantee for at least some level of quality. They don't seem to cover PUT and DELETE requests, though.

Also, the User Contributed Notes to curl_exec() and others contain lots of good information.

Pekka
A: 

Unlike SOAP, REST isn't a standardized protocol so it's a bit difficult to have a "REST Client". However, since most RESTful services use HTTP as their underlying protocol, you should be able to use any HTTP library. In addition to cURL, PHP has these via PEAR:

HTTP_Request2

which replaced

HTTP_Request

A sample of how they do HTTP Basic Auth

// This will set credentials for basic auth
$request = new HTTP_Request2('http://user:[email protected]/secret/');

The also support Digest Auth

// This will set credentials for Digest auth
$request->setAuth('user', 'password', HTTP_Request2::AUTH_DIGEST);
nategood
By REST client I mean something that abstracts away some of the low level details of using curl for http get, post, put, delete etc. which is what I'm doing by building my own php class to do this; I'm wondering if someone has already done this.
Bedwyr Humphreys
Yes, then HTTP_Request_2 may be of interest to you. It abstracts away a lot of the ugliest of cUrl in PHP. To set the method you use, setMethod(HTTP_Request2::METHOD_*). With PUT and POSTs, to set the body of the request you just setBody(<<your xml,json,etc. representation here>>). Authentication described above. It also has abstractions for the HTTP Response (something that cUrl really lacks).
nategood
A: 

I am using curl for basic aurhontication apche but it is not working

kiran