Reputation: 21
I can't get this to work:
<?php
$url = 'http://someServer.com/save.asp?';
$count = 0;
foreach($_POST as $key=>$value)
{
if( $count != 0 ) $url .= "&";
$url .= urlencode($key).'='.urlencode($value);
$count++;
}
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $url;?>">
</head>
<body>
</body>
</html>
If I copy the output of the final $url string and paste it in directly in the browser, it works fine.
So it looks like the $url gets built correctly, but there is some problem with the redirection.
// EDIT
I am trying to get this to work:
<?php
$str = '';
$count = 0;
foreach($_POST as $key=>$value)
{
if( $count != 0 ) $str .= "&";
//$url .= urlencode($key).'='.urlencode($value);
$str .= $key.'='.$value;
$count++;
}
$url = 'http://someDomain.com/acript.asp?'.$str;
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);
?>
I get: Bad Request
Upvotes: 1
Views: 344
Reputation: 190
Ah, your cURL is overly complex, it uses cookies and headers that indicate the request is expecting a an image. simply try this instead:
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);
if this still doesn't work, just use firebug to see what are the headers you browser is sending and add them, like this:
$headers = array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
Upvotes: 0
Reputation: 190
If you want this to work, without relying on the browser to request the page, remove all the HTML and add:
print file_get_contents($url);
at the end. if this doesn't work for you (and it may not work for sevral reasons) you will have to use - http://php.net/manual/en/book.curl.php.
Also to build the right url, you don't have to iterate over $_POST yourself, use: http://php.net/manual/en/function.http-build-query.php
PS what ever you are trying to do, this is probably the wrong solution.
Upvotes: 1