Ответ 1
Проблема с прокси-сервером - это то, с чем сталкивается множество сценариев. Предпочтительным решением, которое я могу найти в Интернете, является просто добавление следующих строк кода.
<?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 => 'http://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 вам нужно поставить на верхнюю часть вашего script следующее:
<?php
namespace FakeCurl;
function curl_exec($ch) {
$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 $response;
}
include("twilio.php");
?>
Если по какой-либо причине произойдет сбой по имени, я бы добавил простую функцию вне клиента twilio.
<?php
function fixProxyResponse($response) {
// 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 $response;
}
И затем измените twilio script 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')
to
if ($response = curl_exec($curl)) {
$parts = explode("\r\n\r\n", fixProxyResponse($response), 3);
list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')