2014-09-30 3 views
0

Я пытаюсь вызвать веб-сервис транспортной компании в своем php-коде и получить результат xml. У меня есть этот образец кода, и я хочу знать, есть ли альтернатива, использующая завиток.Альтернатива fsockopen with curl in php

Код:

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); 
} 

Могу ли я назвать его с помощью завиток?

ответ

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 для Ответить. что вы подразумеваете под «стандартом»? –

+1

Извините, возможно, это сбивало с толку. Стандартным HTTP 1.1 является RFC 2616. Пример, который вы опубликовали, является стандартным HTTP, и это будет работать для этого. –

+0

Мне еще нужно включить заголовки: тип контента/длина и т. Д.?? –

0

Я предполагаю, что полное решение необходимо, но я предлагаю проверить эту базовую CU обертку для PHP https://github.com/shuber/curl