2013-10-28 6 views
16

मेरे पास एक साधारण वेब सेवा है जिसे मैं कनेक्ट करना चाहता हूं। कुछ XML पोस्ट करने के लिए, जो वेब सेवा पक्ष पर ठीक से आगे बढ़ेगा, मुझे उचित अनुरोध तैयार करने की आवश्यकता है। मैं इस प्रकार cURL का उपयोग कर रहा हूं:PHP कर्ल सामग्री-प्रकार सेट नहीं है

try { 
    $ch = curl_init(); 

    if (FALSE === $ch) 
     throw new Exception('failed to initialize'); 

    curl_setopt($ch, CURLOPT_URL,"192.168.1.37"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/xml', 
              'Connection: Keep-Alive' 
              )); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
    curl_setopt($ch, CURLOPT_PROXY, ''); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: ")); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$xml); 


    $request= curl_getinfo($ch); 
    var_dump($request); 


    $content = curl_exec($ch); 


    if (FALSE === $content) 
     throw new Exception(curl_error($ch), curl_errno($ch)); 



} catch(Exception $e) { 

    trigger_error(sprintf(
     'Curl failed with error #%d: %s', 
     $e->getCode(), $e->getMessage()),E_USER_ERROR); 

} 

सबकुछ ठीक होना चाहिए। सबसे पहले मैं अनुरोध शरीर को curl_getinfo($ch); से प्रदर्शित कर रहा हूं और फिर मैं $content चर से प्रतिक्रिया दिखा रहा हूं।

अनुरोध के लिए केवल प्रतिबंध सर्वर के लिए भेजा जा करने के लिए Content-Type शीर्षक है - यह होना चाहिए application/xml अन्यथा सर्वर ने एक त्रुटि जवाब देंगे (यह मेरा विचार नहीं है और मैं इसके बारे में कुछ नहीं कर सकते)

यहाँ उत्पादन अनुरोध है:

array(21) { ["url"]=> string(68) "192.168.1.37" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } }

और प्रतिक्रिया है HTTP 400 Bad Request

मैं से किहै क्या देख सकते हैंलेकिन मैंने इस चर को अपने PHP कोड में सेट किया है ...

क्या कोई मुझे बता सकता है कि मुझे क्या याद आ रही है?

+0

इस पोस्ट को देखने का प्रयास करें: [http://stackoverflow.com/questions/9823810/set-http-request-content-type](http://stackoverflow.com/questions/9823810/set-http- अनुरोध-सामग्री-प्रकार) –

उत्तर

39

आप HTTP_HEADER दो बार सेट कर रहे हैं:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/xml', 
              'Connection: Keep-Alive' 
              )); 

curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: ")); 

मुझे लगता है कि समस्या है। दूसरी बार जब आप इसे सेट करते हैं, तो आप पहली सेटिंग हटाते हैं।

+2

बेशक यह था! विश्वास नहीं कर सकता कि इतनी बेवकूफ गलती थी। धन्यवाद लीजियन! – Mithrand1r

+1

कॉपी पेस्ट कोड के साथ एक मुद्दा उह। मेरे पास एक ही रेखा थी जो सटीक एक ही समस्या थी। upvoted – khuderm