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));

 

 

Link to comment
Share on other sites

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>";
    }
}
?>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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);
        }?>

Link to comment
Share on other sites

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);
?>

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.