Solved

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

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 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