English 中文(简体)
PHP网页请求返回400错误请求
原标题:PHP Webpage Request returns 400 Bad Request
  • 时间:2011-02-07 10:13:15
  •  标签:
  • php

我尝试过使用fopen、file get contents和curl请求,但我总是返回400 Bad request。这是一个带有GET参数的简单网页请求。这是我的代码,有人能理解为什么这会导致问题吗?phpinfo();显示了允许这类请求的配置。

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));

如果我将$forwardURL粘贴到我的浏览器中,它可以正常工作。

输出:

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.

)
最佳回答

尝试使用$forwardURLhttp://php.net/manual/en/function.http-build-query.php“rel=”nofollow“>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 ],
));

这个问题可能是由于URL中具有特殊含义的未转义字符造成的http_build_query为您安全地转义它们。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签