2014-09-30 78 views
0

我想在我的PHP代码中调用一家航运公司的Web服务并获得结果xml。我有这个示例代码,我想知道是否有使用卷曲的替代方法。在PHP中使用curl替代fsockopen

代码:

function doPost($_postContent) { 
    $postContent = "xml_in=".$_postContent; 

    $host="test.company.com"; 
    $contentLen = strlen($postContent); 

    $httpHeader ="POST /shippergate2.asp HTTP/1.1\r\n" 
     ."Host: $host\r\n" 
     ."User-Agent: PHP Script\r\n" 
     ."Content-Type: application/x-www-form-urlencoded\r\n" 
     ."Content-Length: $contentLen\r\n" 
     ."Connection: close\r\n" 
     ."\r\n"; 

    $httpHeader.=$postContent; 


     $fp = fsockopen($host, 81); 

     fputs($fp, $httpHeader); 

     $result = ""; 

     while(!feof($fp)) { 
       // receive the results of the request 
       $result .= fgets($fp, 128); 
     } 

     // close the socket connection: 

     fclose($fp); 

     $result = explode("\r\n\r\n", $result,3); 
} 

我可以调用它使用curl?

回答

1

可以使用CURLOPT_PORT选项来改变端口81.见http://php.net/manual/en/function.curl-setopt.php

$url = "http://test.company.com/shippergate2.asp"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_PORT, 81); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Script"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent); 
$data = curl_exec($ch); 
+0

日Thnx为回复。 “标准”是什么意思? – 2014-09-30 17:43:14

+1

对不起,这可能是令人困惑的。标准的HTTP 1.1是RFC 2616.你发布的例子是标准的HTTP,并且这将适用于此。 – 2014-09-30 18:24:33

+0

我还需要包括标题:内容类型/长度等...? – 2014-10-01 07:59:14