Guzzle

2013-10-22 11 views
8

के लिए इस कर्ल को कनवर्ट करना मैंने गोज़ल डॉक्स के माध्यम से पढ़ने का प्रयास किया है, लेकिन मैं इस समस्या के आसपास अपने सिर को लपेट नहीं सकता।Guzzle

मैं निम्नलिखित के लिए cURL के बजाय Guzzle उपयोग करना चाहते हैं:

protected $url = 'https://secure.abcdef.com/cgi/xml_request_server.php'; 

    $xml = "<ABCRequest>\n"; 
    $xml .=  "<Authentication>\n"; 
    $xml .=   "<ABLogin>$this->gwlogin</ABLogin>\n"; 
    $xml .=   "<ABKey>$this->gwkey</ABKey>\n"; 
    $xml .=  "</Authentication>\n"; 
    $xml .=  "<Request>\n"; 
    $xml .=   "<RequestType>SearchABC</RequestType>\n"; 
    $xml .=  "</Request>\n"; 
    $xml .= "</ABCRequest>\n"; 

    $header = "POST $this->url HTTP/1.1\n"; 
    $header .= "Host: domain.com\n"; 
    $header .= "Content-Length: ".strlen($xml)."\n"; 
    $header .= "Content-Type: text/xml; charset=UTF8\n"; 
    $header .= "Connection: close; Keep-Alive\n\n"; 
    $header .= $xml; 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $this->url); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); 

    $response = curl_exec($ch); 
    curl_close($ch); 

मैं इस कोशिश की, लेकिन मैं अभी भी इस पर नया हूँ ...:

$client = new Client($this->url); 
$response = $client->get($xml); 
+0

उत्तर के अतिरिक्त आपको पहले से ही यह मिलना उपयोगी हो सकता है PHP में स्ट्रिंग का हेरडोक प्रकार: http://php.net/language.types.string.php#language.types.string.syntax.heredoc – hakre

उत्तर

11

आप का उपयोग कर सकते HTTP पोस्ट अनुरोध बनाने के लिए Client::post():

$client = new Client($this->url); 
$request = $client->post(
    '', 
    ['Content-Type' => 'text/xml; charset=UTF8'], 
    $xml, 
    ['timeout' => 120] 
); 
$response = $request->send()->xml();; 
+1

बिल्कुल सही। मैंने इसे xml प्राप्त करने के लिए थोड़ा संशोधित किया: $ request = $ this-> क्लाइंट-> पोस्ट ('', सरणी(), $ xml); \t \t $ प्रतिक्रिया = $ अनुरोध-> भेजें() -> xml(); – Dru

+0

मुझे नहीं लगता कि यह उदाहरण 120 सेकंड की टाइमआउट सेटिंग को कवर करता है और यह अनुरोध निकाय के भीतर सामग्री-प्रकार और वर्णसेट-एन्कोडिंग उपयोग को संबोधित नहीं करता है। – hakre

+0

@hakre एक को अद्यतन करने का प्रयास करें, और दस्तावेज़ पढ़ें - http://guzzlephp.org/http-client/client.html#creating-requests-with-a-client यहां नमूने हैं –

संबंधित मुद्दे