2011-02-07 4 views
1

fopen, 파일 - 가져 오기 - 내용 및 컬 요청을 사용하여 시도했지만 항상 400 잘못된 요청이 반환됩니다. 이것은 GET 매개 변수가있는 간단한 웹 페이지 요청입니다. 여기 내 코드가 있습니다, 왜 이것이 문제를 일으키는 지 알 수 있습니까? phpinfo(); config가 이러한 종류의 요청을 허용한다는 것을 보여줍니다. 내 브라우저에 $ forwardURL을 붙여 넣을 경우PHP 웹 페이지 요청이 400 잘못된 요청을 반환합니다.

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

는 그것을 잘 작동합니다.

출력 :

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. 

) 

답변

4

http_build_query$forwardURL 구축을 시도해보십시오

$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이 안전하게 탈출합니다.

+0

나를 찾아 주셔서 감사합니다! 이스케이프 처리되지 않은 문자에 대해서는 당신이 옳았습니다. 처리되는 동안 요청을 전달하기 전에 이것을 제대로 처리하지 못했습니다. 건배! – Chris

관련 문제