php中fsockopen与curl的替代方法

php中fsockopen与curl的替代方法,php,web-services,sockets,curl,Php,Web Services,Sockets,Curl,我试图用php代码调用一家航运公司的web服务,并获得结果xml。我有这个示例代码,我想知道是否有使用curl的替代方法 代码: 我可以使用curl调用它吗?您可以使用CURLOPT\u端口选项将端口更改为81。看 我想需要一个完整的解决方案,但我建议检查一下PHP的基本CURL包装器thnx以获得答复。你说的“标准”是什么意思?对不起,这可能让人困惑。标准HTTP1.1是RFC2616。您发布的示例是标准HTTP,这将适用于此。我是否还必须包括标题:内容类型/长度等…?cURL将处理内容类型、

我试图用php代码调用一家航运公司的web服务,并获得结果xml。我有这个示例代码,我想知道是否有使用curl的替代方法

代码:


我可以使用curl调用它吗?

您可以使用
CURLOPT\u端口
选项将端口更改为81。看


我想需要一个完整的解决方案,但我建议检查一下PHP的基本CURL包装器

thnx以获得答复。你说的“标准”是什么意思?对不起,这可能让人困惑。标准HTTP1.1是RFC2616。您发布的示例是标准HTTP,这将适用于此。我是否还必须包括标题:内容类型/长度等…?cURL将处理
内容类型
内容长度
主机
、以及
连接
标题。您必须手动设置
用户代理
——我添加了一个示例。
  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);
}
$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);