Solved

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

Posted on 2011-02-16
5
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 110

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

749 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