2013-10-22 27 views
8

我試過閱讀古文字文件,但我無法將這個問題包裹起來。將此捲曲轉換爲古怪文字

我想用狂飲,而不是捲曲以下:

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中的HEREDOC字符串類型:http://php.net/language.types.string.php#language.types.string.syntax.heredoc – hakre

回答

11

您可以使用Client::post()創建一個HTTP 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-> client-> post('',array(),$ xml); ();> xml(); – Dru

+0

我不認爲這個例子覆蓋120秒的超時設置,它沒有解決請求體內的內容類型和字符集編碼的使用。 – hakre

+0

@hakre嘗試更新一個,並閱讀文檔 - http://guzzlephp.org/http-client/client.html#creating-requests-with-a-client這裏是樣本 –