Jump to content

[SOLVED] How to send "Authorization: Basic" header


gtzpower

Recommended Posts

Can anyone tell me why, when using the code below, our server logs are not showing the authorization headers being sent?  Any recommendations on how to get it working?  Thanks!

 

		
$credentials = "calcuser:testpwd";

header("POST /svcs/CalculationEng HTTP/1.0");
header("Content-Type: text/xml; charset=utf-8");
header("Accept: application/soap+xml, application/dime, multipart/related, text/*");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("SOAPAction: \"run\"");
header("Content-Length: 2815");
header("Authorization: Basic " . base64_encode($credentials));

 

 

This might work for you

<?php

if ((!isset($_SERVER['PHP_AUTH_USER'])) || (!isset($_SERVER['PHP_AUTH_PW']))) {
    header('WWW-Authenticate: Basic realm="Secured Area"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required.';
    exit;
} else if ((isset($_SERVER['PHP_AUTH_USER'])) && (isset($_SERVER['PHP_AUTH_PW']))){
    if (($_SERVER['PHP_AUTH_USER'] != "calcuser") || ($_SERVER['PHP_AUTH_PW'] != "testpwd")) {
       header('WWW-Authenticate: Basic realm="Secured Area"');
       header('HTTP/1.0 401 Unauthorized');
       echo 'Authorization Required.';
       exit;
    } else if (($_SERVER['PHP_AUTH_USER'] == "calcuser") || ($_SERVER['PHP_AUTH_PW'] == "testpwd")) {
       echo "<h1>Welcome Friends!</h1>";
    }
}
?>

Thanks for the reply, but I think we're on opposite sides of the fence.  Your code is for the server side while mine is for the client side.  I am connecting to a web service that requires HTTP authentication.  I have to pass the credentials to the remote server to the web service. 

For future readers, this may be of use.

<?php

		$credentials = "username:password";

		// Read the XML to send to the Web Service
		$request_file = "./SampleRequest.xml";
	$fh = fopen($request_file, 'r');
	$xml_data = fread($fh, filesize($request_file));
	fclose($fh);

	$url = "http://www.myservicehost.com/services/calculation";
	$page = "/services/calculation";
	$headers = array(
        	"POST ".$page." HTTP/1.0",
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"run\"",
        "Content-length: ".strlen($xml_data),
        "Authorization: Basic " . base64_encode($credentials)
        );
       
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

        // Apply the XML to our curl call
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 

        $data = curl_exec($ch); 

        if (curl_errno($ch)) {
        	print "Error: " . curl_error($ch);
        } else {
        	// Show me the result
        	var_dump($data);
        	curl_close($ch);
        }?>

Some further stuff regarding pulling an attachment from a soap response.  I noticed the binary data in my case was surrounded by %PDF-1.4 ......... %%EOF.  So I tried the following and I'll be darned if it didn't work :)  I searched all over the net trying to find a way to pull soap attachments without the soap functionality, then decided to just try stuff.  Not sure how consistently reliable it will be, but maybe someone will find it useful...

 

<?php
        	// Show me the result
        	//var_dump($data);
        	
        	$fp = fopen( './file.pdf', 'wb' );
		fwrite( $fp, substr($data,strpos($data,"%PDF-1.4"),(strpos($data,"%%EOF") - strpos($data,"%PDF-1.4") + 5)));
		fclose( $fp );
        	
        	curl_close($ch);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.