Jump to content

Can Php Consume A Coldfusion Rest Service That Requires Username And Password?


crwork

Recommended Posts

This may be a Coldfusion question as well, but I thought I'd throw this out there if anyone has any experience with this...

 

I was asked to consume a Coldfusion service that simply retrieves a user name given an email address. Consuming the service must be done in PHP. Unfortunately, the CF rest service is secured by a username and password.

 

Normally this would be pretty easy, except that Coldfusion requires a "username" and "password" parameter in the authentication header. Coldfusion does this with a CFHTTP call as follows:

 

 

<cfhttp method="get" url="http://testsite.com/...e/usersByEmail/emailhere" username="test" password="test" result="isProfile">

 

If I run just a basic GET request using the cRest client, I get a permissions error because I only use the email address in the URL. It sends back an error message because I'm not sending the username and password. I don't know how to pass the username and password for a GET request.

 

Does anyone know how to do this in PHP, or even just in a REST testing client like cRest?

 

Thanks for any ideas...

Link to comment
Share on other sites

According to Adobes documentation here: http://livedocs.adob...ags_g-h_09.html

 

username/password - Use to pass a username/password to the target URL for Basic Authentication. Forms a base64 encoded string that is passed in the Authenticate header.

 

So if you were to use curl in PHP to make the HTTP request you would use the below options to set the username and password:

 

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, 'username:password');

 

There are many HTTP classes out there if you don't want to use curl or you could use sockets and craft your own HTTP request. Any class/lib that supports RFC2617 should be able to do what you want.

Edited by derwert
Link to comment
Share on other sites

Ok, thanks for the reply. I haven't used curl before, but it sounds like it would do the trick. Thanks for the other options as well. I don't think this server has curl installed, but hopefully it's not a big deal. I'll update the thread once I get results. Or get stuck. :)

Link to comment
Share on other sites

Ok, I was able to connect to the REST service with the Firefox plugin RESTclient. I did a GET request and supplied the username and password in the Basic Authenticate header and results were returned successfully from the REST service.

 

On the development PHP server I've been running cURL tests to no avail however. It's bringing back a curl_errno return code of 0 despite the fact that no data is coming back.

 

I also tried using curl_getinfo() for debugging and oddly enough, the http_code coming back is 302, indicating the URL has moved. However, since I can run the same service with the REST client that brings back data, it makes me think something in the cURL code is malformed.

 

I've attached my cURL code. Anything look awry?

 

 

$ch = curl_init();   
curl_setopt($ch, CURLOPT_URL, 'http://testing.com/myrest/usersByEmail/' . 'testemail.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "theuser:thepassword");
$data = curl_exec($ch);

//debugging displays
echo 'Curl error: ' . curl_error($ch);
echo ', Curl errno: ' .curl_errno($ch);
$info = curl_getinfo($ch);
print_r($info);

curl_close($ch);

 

Related debugging question:

I tried running cURL from the command line in Debian Linux but get the message '-bash curl:command not found'. If I type php -m, 'curl' is in the list; however this server is using the Zend framework so at the bottom of that list it says [Zend Modules]. I can't seem to find where curl is installed on the server. Any thoughts on this one? If not, I can create another thread.

Link to comment
Share on other sites

You're missing the curl_setopt to set the type of HTTP authentication, see my first reply with the sample curl_setopts.

Also see the PHP Manual for curl_setopt http://www.php.net/m...curl-setopt.php

 

CURLOPT_FOLLOWLOCATION - TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).

 

Though it may just be trying to redirect you to the web servers error page. You can capture the packets and view the raw data to see what is happening.

Edited by derwert
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.