Need help for passing authorization information

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mshanthakumari
Forum Newbie
Posts: 1
Joined: Fri Nov 04, 2005 7:59 am

Need help for passing authorization information

Post by mshanthakumari »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have a http query API written in coldfusion will return a .csv file. My PHP script parses the .csv file returned by the coldfusion script. I need to give a default username and password to query the coldfusion API. How will I include the authorization information - username and password in my PHP file before opening the http URL?

Here is the [b]parseSearchAPI.php Code:[/b]

Code: Select all

<?php
$parseSearchAPI = new parseSearchAPI;
$arr = $parseSearchAPI->parseFile("http://xxx-server/searchapi.cfm?string=rme");
print_r($arr);
class parseSearchAPI
{
        function parseFile($filename)
        {
                $fhandle = fopen($filename,"r");
                if ($fhandle)
                {
                        while ($line = fgets($fhandle))
                        {
                                if ($line)
                                {
                                        $elementArr = explode("\"", $line);
                                        $firstPart = trim($elementArr[0]);
                                        $newElementArr = explode(",", $firstPart);
                                        $QueryArr[$newElementArr[0]] = trim($elementArr[1]);
                                }
                        }

                }
                fclose($fhandle);
                return $QueryArr;
        }
}
?>
During execution of parseSearchAPI.php (at command-line) returns the following warning message:

Code: Select all

PHP Warning:  fopen(http://xxx-server/searchapi.cfm?string=rme): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required
 in /parseSearchAPI.php on line 9
PHP Warning:  fclose(): supplied argument is not a valid stream resource in /parseSearchAPI.php on line 24
Content-type: text/html
X-Powered-By: PHP/4.3.4
Please let me know if you need any other information.

Any information on this would be helpful.

Thanks,
Shanthakumari.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

You going to have to use PHP(s) CURL functions because none of the PHP IO handlers f_() by default can handle (COOKIES OR SESSIONS based on cookies). This is really needed if you need LOGIN control! You can do it but it's much more work using f_ type functions! Check the link for examples, if you get stuck post back and I or someone will help you!

yj
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Here is a example... using CURL with cookie control and SSL login support

Code: Select all

<?

/*
 *
 * login url
 *
*/

$login_url = 'https:/site.com/login.asp';

/*
 *
 * login username
 *
*/

$user = 'me';

/*
 *
 * login password
 *
*/

$pass = '1234';

/*
 * change this variable value to what the login form contains
 * including name value pairs to simulate the form. Example..
 * say the form on the login page has this...
 *
 * <input type='text' name='username' value=''>
 * <input type='password' name='password' value=''>
 *
 * Then the $login_form would contain what the example below
 * contains.
*/

$login_form = "username=" . $user . "&password=" . $pass;

/*
 *
 * cookie jar, path and file name
 *
*/

$cookie_jar = 'cookie.txt';

/*
 *
 * download file url (the file you want to download)
 *
*/

$download_url = 'https://www.secureSite.net/thePageINeed.asp?id=2344';

/*
 *
 * save the download file to...
 *
*/

$save_file = 'save.txt';


/* run the script... */

     $io = curl_init ();

curl_setopt ( $io, CURLOPT_URL, $login_url );
curl_setopt ( $io, CURLOPT_POST, 1 );
curl_setopt ( $io, CURLOPT_POSTFIELDS, $login_form );
curl_setopt ( $io, CURLOPT_COOKIEJAR, $cookie_jar );
curl_setopt ( $io, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ( $io, CURLOPT_SSL_VERIFYPEER, false );

     $out = curl_exec ( $io );

curl_setopt ( $io, CURLOPT_URL, $download_url );

     $new = curl_exec ( $io );

curl_close ( $io );

/* $new contains the downloaded page or file */

$io = fopen ( $save_file, 'wb' );
fputs ( $io, $new );
fclose ( $io );

unlink ( $cookie_jar );

?>



yj
Post Reply