gtzpower Posted January 4, 2008 Share Posted January 4, 2008 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)); Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/ Share on other sites More sharing options...
sKunKbad Posted January 4, 2008 Share Posted January 4, 2008 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/#findComment-430509 Share on other sites More sharing options...
gtzpower Posted January 4, 2008 Author Share Posted January 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/#findComment-430615 Share on other sites More sharing options...
gtzpower Posted January 7, 2008 Author Share Posted January 7, 2008 bump? Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/#findComment-432670 Share on other sites More sharing options...
gtzpower Posted January 7, 2008 Author Share Posted January 7, 2008 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); }?> Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/#findComment-432808 Share on other sites More sharing options...
gtzpower Posted January 7, 2008 Author Share Posted January 7, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84480-solved-how-to-send-authorization-basic-header/#findComment-432933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.