2013-01-04 28 views
20

तो, लंबी कहानी छोटी है, मेरे पास एक AJAX एप्लिकेशन है जो एमवीसी वेब एपीआई को बैक एंड के रूप में उपयोग करता है। क्लाइंट हालांकि एक अलग डोमेन से कॉल करता है और क्रॉस-डोमेन अनुरोध समस्याओं को प्राप्त करने के लिए PHP प्रॉक्सी फ़ाइल का उपयोग करता है।मैं 100 जारी रखने से curl कैसे रोक सकता हूं?

हालांकि, PHP प्रॉक्सी का उपयोग करते हुए, वेब एपीआई 100 Continue HTTP शीर्षलेख के साथ कुछ अनुरोधों का जवाब देता है और कोई भी अनुरोध जो इसे वापस पाने के लिए अत्यधिक समय लेता है (हम 2 मिनट तक बात कर रहे हैं) और यह भी कर सकते हैं एक गैर वैध प्रतिक्रिया वापस करें।

यह appears to be a known issue with cURL और वैकल्पिक हल आमतौर पर उम्मीद को दूर करने के लिए नीचे दिए गए लाइन डालने के रूप में उद्धृत किया गया है: cURL अनुरोध

दुर्भाग्य में 100 शीर्ष, समाधान मेरे लिए मायावी हो रहा है:

$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:')); 

यह कोड काम करता है लेकिन अन्य सभी शीर्षकों को हटा देता है (जो मेरे लिए व्यावहारिक नहीं है क्योंकि मैं एपीआई के साथ प्रमाणित करने के लिए HTTP मूल ऑथ हेडर का उपयोग कर रहा हूं)। आप यह भी ध्यान दे सकते हैं कि मैंने मौजूदा शीर्षकों में Expect: जोड़ने का प्रयास किया, लेकिन इससे मेरी मदद नहीं हुई।

मैं मौजूदा शीर्षकों को कैसे बनाए रख सकता हूं, लेकिन 100 को जारी रखने से curl को भी रोक सकता हूं?

+2

'getallheaders()' क्या आप यह करता है लगता है लगता है ऐसा नहीं करता है:

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 

यहाँ स्क्रिप्ट है: जब से मैं HTTP PUT के लिए इस की जरूरत है, मैं इसे निम्नलिखित परिणामों के साथ एक सा बढ़ाया । यह अनुरोध हेडर प्राप्त करता है जो आपको भेजे गए थे, अनुरोध हेडर नहीं जो कर्ल का उपयोग करेंगे। आपको केवल 'curl_setopt ($ ch, CURLOPT_HTTPHEADER, सरणी (' अपेक्षा: ') की आवश्यकता होनी चाहिए); 'रेखा - उपरोक्त कोड से बाकी सब कुछ हटा दें और इसे काम करना चाहिए। – DaveRandom

+0

डेव रैंडम - असल में यह वास्तव में इरादा है - मैं अनुरोध हेडर ले रहा हूं और उन्हें एपीआई पर भेज रहा हूं (उनमें HTTP एथ सामान शामिल हैं)। समस्या यह है कि curl इन शीर्षकों के लिए 'उम्मीद: 100' _add_ होगा। – jmillar

+0

[इस] का जिक्र करते हुए (http://stackoverflow.com/questions/2964687/how-to-handle-100-continue-http-message) क्या आप 'curl_setopt ($ ch, CURLOPT_FAILONERROR, TRUE) जोड़ने का प्रयास कर सकते हैं; '? उम्मीद है की यह मदद करेगा। – user1190992

उत्तर

19

$headers_new[] = 'Expect:'; का उपयोग करते हुए काम करता है जब तक$headers_new सरणी एक स्ट्रिंग है है 'Expect: 100-continue' शामिल हैं। इस मामले में आपको इसे सरणी से निकालना होगा अन्यथा यह 100 जारी (तार्किक रूप से) की अपेक्षा करेगा।

क्योंकि आपके कोड में आप getallheaders() का उपयोग करते हैं और आप यह जांच नहीं रहे हैं कि इसमें Expect: 100-continue शीर्षलेख पहले से है, तो शायद यह आपके मामले में मामला है।

यहां सामान्य स्थिति (और स्क्रिप्ट है कि यह बनाया गया) के लिए एक सारांश है:

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 

कोड:

<?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

मुझे एक संबंधित, लेकिन पुरानी पोस्ट मिली: http://stackoverflow.com/a/463277/367456 – hakre

0

स्क्रिप्ट, Hakre के लिए धन्यवाद।

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

?>