Chris
Chris

Reputation: 2344

PHP Webpage Request returns 400 Bad Request

I have tried using fopen, file-get-contents and a curl request but I always get 400 Bad Request returned. This is a simple webpage request with GET params. Here is my code, can anyone see why this would be causing problems? phpinfo(); shows the config allows these sorts of requests.

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
$this->headers = array(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, image/gif, image/x-bitmap, image/jpeg, image/pjpeg',
    'Connection: Keep-Alive',
    'Content-type: application/x-www-form-urlencoded;charset=UTF-8'
);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

....

        $forwardURL = '*****/receive?from='.$_GET["from"].'&msg='.$_GET['msg'].'&username='.$_GET['username'].'&ac_password='.$_GET['ac_password'].'&to='.$_GET['to'].'&do='.$_GET['do'];
print_r($this->get_web_page($forwardURL));

If I paste $forwardURL into my browser it works fine.

output:

Array ( [url] => http://************/receive?from=*****&msg=*********&username=*******&ac_password=*****&to=********&do=send [content_type] => text/html; charset=us-ascii [http_code] => 400 [header_size] => 179 [request_size] => 594 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.053303 [namelookup_time] => 0.000996 [connect_time] => 0.025709 [pretransfer_time] => 0.025711 [size_upload] => 0 [size_download] => 311 [speed_download] => 5834 [speed_upload] => 0 [download_content_length] => 311 [upload_content_length] => 0 [starttransfer_time] => 0.053264 [redirect_time] => 0 [errno] => 0 [errmsg] => [content] =>
Bad Request

HTTP Error 400. The request is badly formed.

)

Upvotes: 1

Views: 3872

Answers (1)

lonesomeday
lonesomeday

Reputation: 237975

Try building $forwardURL with http_build_query:

$forwardURL = '*****/receive?' . http_build_query(array(
    'from' => $_GET['from'],
    'msg' => $_GET['msg'],
    'username' => $_GET['username'],
    'ac_password' => $_GET['password'],
    'to' => $_GET['to'],
    'do' => $_GET['do'],
));

The problem is probably due to unescaped characters that have special meanings in URLs. http_build_query escapes them for you safely.

Upvotes: 4

Related Questions