views:

155

answers:

1

I am working on setting up credit card processing for a site that is live. PHP wasn't compiled with CURL support and I don't want to take the site down to recompile PHP with that, so I am trying to use a different method than the example code provided.

Example CURL code:

function send($packet, $url) {
    $header = array("MIME-Version: 1.0","Content-type: application/x-www-form-urlencoded","Contenttransfer-encoding: text"); 
    $ch = curl_init();

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
    // Uncomment for host with proxy server
    // curl_setopt ($ch, CURLOPT_PROXY, "http://proxyaddress:port"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $packet); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 

    // send packet and receive response
    $response = curl_exec($ch); 
    curl_close($ch); 
    return($response);
}

My attempt to use a different method:

function send($packet, $host, $path) {
    $content = '';
    $flag = false;
    $post_query = urlencode($packet) . "\r\n";
    $fp = fsockopen($host, '80');
    if ($fp) {
        fputs($fp, "POST $path HTTP/1.0\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n");
        fputs($fp, $post_query);

        while (!feof($fp)) {
            $line = fgets($fp, 10240);
            if ($flag) {
                $content .= $line;
            } else {
                $headers .= $line;
                if (strlen(trim($line)) == 0) {
                    $flag = true;
                }
            }
        }
        fclose($fp);
    }

    return $content;
}

While this does actually give me a return from the URL I am calling, it does so incorrectly because I get an error back saying it was formatted incorrectly. I am not very familiar with either CURL or this other method, so I am not sure what I am doing wrong.

A: 

Though you specifically asked for fsockopen() you might also want to try the stream api and context options

function send($packet, $url) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>$packet
      )
    )
  );
  return file_get_contents($url, 0, $ctx);
}

if you need more error handling use fopen() and stream_get_meta_data() instead of file_get_contents()

VolkerK
Thank you! That worked perfectly.
James Simpson
Hmm, actually this doesn't work with HTTPS, is there a way to make that work?
James Simpson
Oh https. Yes ....but you need a module that provides the ssl part of https. And that's what you wanted to avoid: restart the webserver in order to have php load another module. Most "standard" versions of php use the openssl-extension for that, http://docs.php.net/openssl but it could also be ...taadaa... the curl extension. I somewhat doubt that you'll find a production-level "pure" php implementation of the ssl transport layer. Could be wrong though.
VolkerK