?
Solved

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

Posted on 2011-02-16
5
Medium Priority
?
372 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 111

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 1800 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

801 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