I am using the following script to post data to an external site. In the example below, $method, $url, and $postdata have already been gathered by the user.
foreach($postdata as $key => $value)
{
$data .= $key . '=' . $value . '&';
}
if ($method == 'POST') {
// Set headers
$headers = array('http' => array(
'method' => $method,
'header' => "accept-language: en\r\n" .
"Host: $host\r\n" .
"Referer: $url\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $data));
// Send request and retreive response
$context = stream_context_create($headers);
$fp = fopen($url, 'rb', false, $context);
fpassthru($fp);
fclose($fp);
} else if ($method == 'GET') {
// Set headers
$headers = array('http' => array(
'method' => $method,
'header' => "accept-language: en\r\n" .
"Host: $host\r\n" .
"Referer: $url\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n"));
// Append url
$url .= '?' . $data;
// Send request and retreive response
$context = stream_context_create($headers);
$fp = fopen($url, 'rb', false, $context);
fpassthru($fp);
fclose($fp);
} else {
echo 'Invalid method.';
}
It works fine in most cases, however some sites will send back a 403 Forbidden, ostensibly because they don’t like fopen() requests. Is there a way around this? If I use cURL instead will it prevent the 403? And, if so, what would be the cURL way of doing the above? Thanks.
You may want to ensure you pass a USER-AGENT along with your headers. I’ve worked on projects which required us to log into remote systems so we could pull data, but on some of these systems, if we didn’t pass a valid user-agent we would not gain access, even though the login credentials were set correctly.