2013-01-04 23 views
20

Vì vậy, câu chuyện dài ngắn, tôi có một ứng dụng AJAX sử dụng MVC Web API như là kết thúc trở lại. Tuy nhiên, khách hàng gọi từ một miền khác và sử dụng tệp proxy PHP để giải quyết các vấn đề yêu cầu miền chéo.Làm cách nào để ngăn cURL sử dụng 100 Tiếp tục?

Tuy nhiên, bằng cách sử dụng proxy PHP, API Web phản hồi các yêu cầu nhất định với tiêu đề HTTP 100 Continue và mọi yêu cầu nhận lại quá nhiều thời gian này (chúng tôi đang nói đến 2 phút) và cũng có thể trả về phản hồi không hợp lệ.

này appears to be a known issue with cURL và thực hiện giải pháp thường được trích dẫn là chèn dòng dưới đây để loại bỏ các mong đợi: 100 tiêu đề trong yêu cầu cURL

Thật không may, giải pháp có vẻ là khó nắm bắt cho tôi:

$headers = getallheaders(); 
$headers_new = ""; 
foreach($headers as $title => $body) { 
    $headers_new[] = $title.": ".$body; 
} 
//$headers_new[] = 'Expect:'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_new); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

Mã này hoạt động nhưng loại bỏ tất cả các tiêu đề khác (không khả thi đối với tôi khi tôi đang sử dụng tiêu đề xác thực cơ bản HTTP để xác thực với API). Bạn cũng có thể nhận thấy rằng tôi đã thử thêm Expect: vào các tiêu đề hiện tại, nhưng điều này cũng không giúp ích gì cho tôi.

Làm cách nào để duy trì các tiêu đề hiện tại mà còn ngăn không cho cURL mong đợi 100 tiếp tục?

+2

'getallheaders() 'không làm những gì bạn dường như nghĩ rằng nó không . Nó nhận được các tiêu đề yêu cầu được gửi đến cho bạn, không phải các tiêu đề yêu cầu mà curl sẽ sử dụng. Bạn chỉ cần 'curl_setopt ($ ch, CURLOPT_HTTPHEADER, mảng ('Mong đợi:'));' dòng - xóa mọi thứ khác khỏi mã ở trên và nó sẽ hoạt động. – DaveRandom

+0

DaveRandom - thực sự đó là ý định chính xác - tôi lấy các tiêu đề yêu cầu và chuyển chúng vào API (chúng bao gồm các công cụ HTTP Auth). Vấn đề là cURL sẽ _add_ the 'Expect: 100' vào các tiêu đề này. – jmillar

+0

Đề cập đến [this] (http://stackoverflow.com/questions/2964687/how-to-handle-100-continue-http-message) bạn có thể thử thêm 'curl_setopt ($ ch, CURLOPT_FAILONERROR, TRUE);'? Hi vọng điêu nay co ich. – user1190992

Trả lời

19

Sử dụng $headers_new[] = 'Expect:'; không hoạt động trừ khi mảng $headers_new chứa chuỗi là 'Expect: 100-continue'. Trong trường hợp này, bạn cần phải loại bỏ nó khỏi mảng nếu không nó sẽ được mong đợi 100 tiếp tục (hợp lý).

Vì trong mã của bạn, bạn sử dụng getallheaders() và bạn không kiểm tra xem nó có chứa tiêu đề Expect: 100-continue do đó có thể là trường hợp này trong trường hợp của bạn.

Dưới đây là một bản tóm tắt về tình hình chung (và kịch bản đã tạo ra nó):

PHP Curl HTTP/1.1 100 Continue and CURLOPT_HTTPHEADER 

GET request ..........................................: Continue: No 
GET request with empty header ........................: Continue: No 
POST request with empty header .......................: Continue: Yes 
POST request with expect continue explicitly set .....: Continue: Yes 
POST request with expect (set to nothing) as well ....: Continue: Yes 
POST request with expect continue from earlier removed: Continue: No 

Code:

<?php 

$ch = curl_init('http://www.iana.org/domains/example/'); 

function curl_exec_continue($ch) { 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    $continue = 0 === strpos($result, "HTTP/1.1 100 Continue\x0d\x0a\x0d\x0a"); 
    echo "Continue: ", $continue ? 'Yes' : 'No', "\n"; 

    return $result; 
} 

echo "GET request ..........................................: ", !curl_exec_continue($ch); 

$header = array(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "GET request with empty header ........................: ", !curl_exec_continue($ch); 

curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array('hello')); 
echo "POST request with empty header .......................: ", !curl_exec_continue($ch); 

$header[] = 'Expect: 100-continue'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect continue explicitly set .....: ", !curl_exec_continue($ch); 

$header[] = 'Expect:'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect (set to nothing) as well ....: ", !curl_exec_continue($ch); 

unset($header[0]); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect continue from earlier removed: ", !curl_exec_continue($ch); 
+0

Tôi đã tìm thấy bài đăng có liên quan nhưng cũ hơn: http://stackoverflow.com/a/463277/367456 – hakre

0

Cảm ơn các kịch bản, Hakre. Kể từ khi tôi cần thiết này cho HTTP PUT, tôi mở rộng nó một chút với kết quả như sau:

GET request ..........................................: Continue: No 
GET request with empty header ........................: Continue: No 
POST request with empty header .......................: Continue: Yes 
POST request with expect continue explicitly set .....: Continue: Yes 
POST request with expect (set to nothing) as well ....: Continue: Yes 
POST request with expect continue from earlier removed: Continue: No 
PUT request with empty header ........................: Continue: Yes 
PUT request with expect continue explicitly set ......: Continue: Yes 
PUT request with expect (set to nothing) as well .....: Continue: Yes 
PUT request with expect continue from earlier removed : Continue: No 
DELETE request with empty header .....................: Continue: Yes 
DELETE request with expect continue explicitly set ...: Continue: Yes 
DELETE request with expect (set to nothing) as well ..: Continue: Yes 
DELETE request with expect continue from earlier removed : Continue: No 

Đây là kịch bản:

<?php 

$ch = curl_init('http://www.iana.org/domains/example/'); 

function curl_exec_continue($ch) { 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    $continue = 0 === strpos($result, "HTTP/1.1 100 Continue\x0d\x0a\x0d\x0a"); 
    echo "Continue: ", $continue ? 'Yes' : 'No', "\n"; 

    return $result; 
} 

// --- GET 

echo "GET request ..........................................: ", !curl_exec_continue($ch); 

$header = array(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "GET request with empty header ........................: ", !curl_exec_continue($ch); 

// --- POST 

curl_setopt($ch, CURLOPT_POST, TRUE); 

curl_setopt($ch, CURLOPT_POSTFIELDS, array('hello')); 
echo "POST request with empty header .......................: ", !curl_exec_continue($ch); 

$header[] = 'Expect: 100-continue'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect continue explicitly set .....: ", !curl_exec_continue($ch); 

$header[] = 'Expect:'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect (set to nothing) as well ....: ", !curl_exec_continue($ch); 

unset($header[0]); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "POST request with expect continue from earlier removed: ", !curl_exec_continue($ch); 

// --- PUT 

curl_setopt($ch, CURLOPT_PUT, TRUE); 

$header = array(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "PUT request with empty header ........................: ", !curl_exec_continue($ch); 

$header[] = 'Expect: 100-continue'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "PUT request with expect continue explicitly set ......: ", !curl_exec_continue($ch); 

$header[] = 'Expect:'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "PUT request with expect (set to nothing) as well .....: ", !curl_exec_continue($ch); 

unset($header[0]); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "PUT request with expect continue from earlier removed : ", !curl_exec_continue($ch); 

// --- DELETE 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 

$header = array(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "DELETE request with empty header .....................: ", !curl_exec_continue($ch); 

$header[] = 'Expect: 100-continue'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "DELETE request with expect continue explicitly set ...: ", !curl_exec_continue($ch); 

$header[] = 'Expect:'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "DELETE request with expect (set to nothing) as well ..: ", !curl_exec_continue($ch); 

unset($header[0]); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
echo "DELETE request with expect continue from earlier removed : ", !curl_exec_continue($ch); 

?> 
Các vấn đề liên quan