Hi,

In one of my PHP scripts, I have been using the CURL library to make a POST call to an external script as per the code below?

On a particular shared URL, CURL is not installed(nor is it an option to be installed at this point in time) - is there an alternative? My brief investigation suggests that 'fopen' might be the way to go (allow_url_fopen is turned on in PHP config).

Can someone provide the equivalent example using 'fopen'? Is there a better alternative?

Thanks,
Adam

PS, here is my CURL code:

$ch = curl_init(); // initialize curl handle
$url = "http://www.mydomain.com/cgi-bin/calc.cgi"; // URL to calc.cgi
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "amount=$total&ref=xyz"); // fields to POST to calc.cgi
$data = curl_exec($ch); // run the whole process
curl_close($ch);

    try this:

    $fp = @fsockopen("www.mydomain.com", 80, $errno, $errstr, 10);
    if(!$fp) {
        echo "Error $errno: $errstr<br />\n";
        exit;
    }
    
    $postdata = "amount=$total&ref=xyz";
    $postdata = urlencode($postdata);
    
    $data = "POST /cgi-bin/calc.cgi HTTP/1.0\r\n";
    $data .= "Host: www.mydomain.com\r\n";
    $data .= "Content-type: application/x-www-form-urlencoded\r\n";
    $data .= "Content-length: " . strlen($postdata) . "\r\n";
    $data .= "\r\n";
    $data .= $postdata;
    $data .= "\r\n";
    
    fputs($fp, $data);
    
    while(!feof($fp)) {
        $return .= fgets($fp);
    }
    
    fclose($fp);
    
    echo $return;
    

    that in general should do what you want. you also have the options to throw in fake referers and user agents if necessary for the calc.cgi script.

      Write a Reply...