doupuzhimuhan9216 2010-12-22 14:20
浏览 62
已采纳

PHP - 奇怪的问题 - 由于看似无关的代码行而损坏的标头/内容

I have spent 3 days & 4 SO questions trying to fix something which didn't have a problem in the first place. However, the actual problem now has me stumped. Please see the following curl code. It correctly fetches a particular webpage and displays. There is a commented line (no. 8), which sets an additional POST variable. If I uncomment it, then the browser tries to download a gzip file instead of displaying it. I don't know the correlation between that line and the strange behaviour at all.

(Note: I have used a static url from w3schools so others can try this code. I am trying to use this code for my own internal data server and proxy, and am facing exactly same issue. Uncommenting that particular line results in the strange behaviour. I need to use this variable. I don't presently know a work-around plus am very curious to find the cause).

<?php
session_start();

$i_var_prefix="i_var_";

// Other important client dependent 'SERVER' variables.
if(isset($_SERVER['HTTPS'])) { $_POST["${i_var_prefix}_HTTPS"]=$_SERVER['HTTPS']; };
//if(isset($_SERVER['REMOTE_ADDR'])) { $_POST["${i_var_prefix}_REMOTE_ADDR"]=$_SERVER['REMOTE_ADDR']; };
// STRANGE PROBLEM:: IF I UNCOMMENT THE LINE ABOVE, THEN THE BROWSER DOES NOT DISPLAY THE CONTENT BUT TRIES TO DOWNLOAD IT.

$curl_url="http://www.w3schools.com/TAGS/form_action.asp";


// Set values of these header variables as got from client
$field_array= array(
      'Accept' => 'HTTP_ACCEPT',
      'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
      'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
      'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
      'Connection' => 'HTTP_CONNECTION',
      'Host' => 'HTTP_HOST',
      'Referer' => 'HTTP_REFERER',
      'User-Agent' => 'HTTP_USER_AGENT'
      );

$curl_request_headers=array();

foreach ($field_array as $key => $value) {
   if(isset($_SERVER["$value"])) {
      $server_value=$_SERVER["$value"];
      $curl_request_headers[]="$key: $server_value";
   }
};

//------
session_write_close();

//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE,session_name()."=".session_id().";");
//Set the url, number of POST vars, POST data
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_POST, count($_POST));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curl_request_headers);


//Execute post
$result = curl_exec($curl_handle);

//Close connection
curl_close($curl_handle);

list($headers,$content)=explode("

",$result,2);
foreach (explode("
",$headers) as $hdr) {
   header($hdr);
}
echo $content;
?>

UPDATE:

With the line uncommented, the result headers received are:

HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Wed, 22 Dec 2010 14:32:43 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
Content-Length: 478
Content-Type: text/html
Set-Cookie: ASPSESSIONIDSASCDCDT=KIIKANGALGLDJMLFHGPJBBOM; path=/
Cache-control: private

With the line commented out, result headers are:

HTTP/1.1 200 OK
Date: Wed, 22 Dec 2010 14:34:21 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
Content-Length: 478
Content-Type: text/html
Set-Cookie: ASPSESSIONIDSASCDCDT=JLPKANGAHDCNADBMNGHGIMCO; path=/
Cache-control: private

1) Why the difference?

2) How to handle the continue thing correctly?


SUMMARY:

The reason was that with that line, the number of POST variables increased to more than 1 and curl started automatically sending "Expect:" in header. This made the server respond with "Continue" header, which I was not handling. I am using the solution posted below. The comments, specially Mchl's comments - were very helpful in getting me in the right direction as I had no clue how that line could affect the behaviour.

regards,

JP

  • 写回答

3条回答 默认 最新

  • dr637349 2010-12-22 15:00
    关注

    Found one solution through some guesswork and searching. From http://matthom.com/archive/2008/12/29/php-curl-disable-100-continue-expectation. The 100-continue status is "expected" by default when using cURL-

    Expect: 100-continue.

    I disabled it using

    $curl_request_headers[]="Expect: ";.

    And now it works! (I guess server stops sending the continue header). (However, I am not convinced this is robust for all types of requests that the client can make [in the long run] but might be okay for normal purpose).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛