php – 如何处理来自代理的额外HTTP头?

前端之家收集整理的这篇文章主要介绍了php – 如何处理来自代理的额外HTTP头?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们的环境需要使用外部代理进行异地服务.通常这不是问题.在这种情况下,使用Twilio,返回的额外标头会中断客户端.

传出标题

POST /2010-04-01/Accounts/FOO/SMS/Messages.json HTTP/1.1
Authorization: Basic FOO==
User-Agent: twilio-PHP/3.10.0
Host: api.twilio.com
Accept: */*
Accept-Charset: utf-8
Content-Type: application/x-www-form-urlencoded
Content-Length: 108

回应标题

HTTP/1.0 200 Connection established

HTTP/1.1 201 Created
Server: Nginx
Date: Thu,06 Jun 2013 14:39:24 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 551
Connection: close
X-Powered-By: PHP/5.3.11

我只能假设代理是添加额外的HTTP头.

Twilio客户端确实检查:

list($head,$body) = ($parts[0] == 'HTTP/1.1 100 Continue')

据了解,有些卷曲的版本会自动在请求中添加一个Expect标头,而HTTP 100将在响应中返回,但在这种情况下,它不是,并且响应是200 Connection已建立.为什么值得添加一个空的Expect:或者Expect:培根没有改变结果.

我真的不想在这里劫持Twilio客户端,我特别想避免只添加一个|| $parts [0] ==’HTTP / 1.0 200连接建立’,因为它似乎是凌乱的.

是否可以发送一个请求头来抑制/隐藏额外的头?或者,卷曲选项我看不到忽视它?

出站代理是Linux / Squid

代理问题是许多脚本面临的问题.在互联网上找到的首选解决方案是简单地添加以下代码行.
<?PHP
// cURL automatically handles Proxy rewrites,remove the "HTTP/1.0 200 Connection established" string
if (false !== stripos($response,"HTTP/1.0 200 Connection established\r\n\r\n")) {
  $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n",'',$response);
}
?>

现在添加到twilio客户端会确实有点凌乱.幸运的是,您可以使用命名空间重新创建本机功能.请参见以下示例.

<?PHP
namespace FakeCurl;

//create curl_exec function with same name,but its created in the FakeCurl namespace now.
function curl_exec($ch) {
  //execute the actual curl_exec function in the main namespace
  $response =  \curl_exec($ch);

  // cURL automatically handles Proxy rewrites,remove the "HTTP/1.0 200 Connection established" string
  if (false !== stripos($response,"HTTP/1.0 200 Connection established\r\n\r\n")) {
    $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n",$response);
  } 

  return "ADDED TO RESPONSE\r\n\r\n".$response;
}

//make a regular curl request,no alterations.

$curl = curl_init();
curl_setopt_array( $curl,array(
    CURLOPT_HEADER => true,CURLOPT_NOBODY => true,CURLOPT_RETURNTRANSFER => true,CURLOPT_URL => 'https://stackoverflow.com' ) );
$response = curl_exec( $curl );
curl_close( $curl );

echo '<pre>'.$response.'</pre>';

?>

产量

ADDED TO RESPONSE

HTTP/1.1 200 OK
Cache-Control: public,max-age=11
Content-Length: 191951
Content-Type: text/html; charset=utf-8
Expires: Wed,12 Jun 2013 07:09:02 GMT
Last-Modified: Wed,12 Jun 2013 07:08:02 GMT
Vary: *
X-Frame-Options: SAMEORIGIN
Date: Wed,12 Jun 2013 07:08:49 GMT

所以要与twilio客户端一起使用,你需要放在脚本的最上方如下:

<?PHP
namespace FakeCurl;
function curl_exec($ch) {
  $response =  \curl_exec($ch);

  // cURL automatically handles Proxy rewrites,$response);
  } 

  return $response;
}

include("twilio.PHP");
?>

如果命名空间选项由于某种原因失败,我将在twilio客户端之外添加一个简单的函数.

<?PHP
function fixProxyResponse($response) {
  // cURL automatically handles Proxy rewrites,$response);
  } 

  return $response;
}

然后改变twilio脚本TinyHttp.PHP并且只改变以下行(〜linenr 63)

if ($response = curl_exec($curl)) {
  $parts = explode("\r\n\r\n",$response,3);
  list($head,$body) = ($parts[0] == 'HTTP/1.1 100 Continue')

if ($response = curl_exec($curl)) {
  $parts = explode("\r\n\r\n",fixProxyResponse($response),$body) = ($parts[0] == 'HTTP/1.1 100 Continue')

猜你在找的PHP相关文章