mike_x_
mike_x_

Reputation: 1929

Alternative of fsockopen with curl in php

I am trying to call a web service of a shipping company in my php code and get the result xml. I have this sample code and i want to know if there is an alternative using curl.

Code:

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

Can i call it using curl?

Upvotes: 0

Views: 2789

Answers (2)

Jeremiah Winsley
Jeremiah Winsley

Reputation: 2557

You can use the CURLOPT_PORT option to change the port to 81. See 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);

Upvotes: 1

ad4s
ad4s

Reputation: 304

I guess a complete solution is needed but I suggest checking this basic CURL wrapper for PHP https://github.com/shuber/curl

Upvotes: 0

Related Questions