Solved

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

Posted on 2011-02-16
5
343 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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now