Solved

php - post a form to another server with http auth and get result

Posted on 2011-02-16
5
349 Views
Last Modified: 2012-05-11
Hi,

I'm on a server, lets say, "www.example.com" and want to post a form to "www.example2.com/page".

"www.example2.com/page" need http authentication to be accessible.

When posting a form to this via POST-method it returns xml.

I want to get this xml and put it in a string.

How can this be done?

If it helps, "www.example2.com/page" return this when just trying to send form data to it.

    [0] => HTTP/1.1 401 Authorization Required
    [1] => Date: Wed, 16 Feb 2011 14:37:52 GMT
    [2] => Server: Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.7l DAV/2 PHP/5.2.14
    [3] => WWW-Authenticate: Basic realm="WebNative"
    [4] => Content-Length: 401
    [5] => Content-Type: text/html; charset=iso-8859-1

Please ask if it's unclear what i want to do.

The server sending the form uses php 5.

Included my code, if you don't understand the question, maybe you'll see what i'm trying to do :)
$hostname = $_POST['server_NOSUBMIT'];// 'example2.com'

$cgi = $_POST['file_NOSUBMIT']; // '/page'

$my_data;
foreach($_POST as $key =>$value){
    $mydata[$key] = $value;
}

$send_string =
'POST '.$cgi.' HTTP/1.1' . "\r\n" .
'Host: '.$hostname. "\r\n" .
'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
'WWW-Authenticate: Basic realm="WebNative"' . "\r\n" .
'Content-Length: ';

$data_string = "";
$add_ampersand = FALSE;
foreach ($_POST as $key => $value)
{
    if ($add_ampersand)
    {
        $data_string .= '&';
    }
    $data_string .= $key . '=' . $value;
    $add_ampersand = TRUE;
}

$send_string .= strlen($data_string) ."\r\n\r\n" . $data_string . "\r\n\r\n";

$fh = fsockopen ($hostname, 80);

fputs ($fh, $send_string);

$return = fread ($fh, 4096);

fclose ($fh);

$return_array = preg_split ('/\r\n/', $return);

Open in new window

0
Comment
Question by:Gudorian
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34907143
What is unclear is the ACTUAL URL and the test-data version of the authentication credentials.  Using an imaginary web site as an example is not helpful.  Please give us some test data so we can illustrate the practical solution, thanks.
0
 
LVL 3

Expert Comment

by:WebCricket
ID: 34907217
You could actually use CURL to send the post string instead of doing an fsocketopen. CURL has the natural ability to send a post string and then do a return transfer of all data that is received from the outside server. Check this site out for more on CURL. http://blog.unitedheroes.net/curl/
0
 
LVL 8

Accepted Solution

by:
rationalboss earned 450 total points
ID: 34907249
<?php
function postme($data, $username, $password) {
  $context = stream_context_create(array(
    'http' => array(
      'method'  => 'POST',
      'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
                   "Content-type: application/x-www-form-urlencoded\r\n",
      'content' => http_build_query(array(
					'data' => $data,
					'data2' => 'another data here',
					'data3' => 'blah'
					)),
      'timeout' => 5,
    ),
  ));
  $ret = file_get_contents('http://example2.com/page', false, $context);
  return $ret;
}

echo postme('hello world','myusername','mypassword');
?>

Open in new window

0
 
LVL 3

Assisted Solution

by:WebCricket
WebCricket earned 50 total points
ID: 34907299

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://example2.com'); //the url that you want to retrieve data from
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat"); //the post string that you will send to the other server in url format
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); //sets the return transfer option to true, meaning that curl will return whatever data is output by the other server
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

Open in new window

0
 
LVL 2

Author Closing Comment

by:Gudorian
ID: 34907430
Thanks for all your help, I'll go with rationalboss solution since it was complete with the authorization.
It works perfectly.

Thanks WebCricket for the curl example.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question