Page 1 of 1

sendToHost php script

Posted: Tue Jul 17, 2007 8:12 am
by superdez
I'm trying to submit to my payment processor using this script I found on the internet but when I execute it, the browser appears blank with no error codes. Any ideas ? Even the "examples" appear blank with no errors.

I use this line to execute the function:

Code: Select all

sendToHost('select.worldpay.com','post','/wcc/purchase','instId=x&cartId=x');


*************************************

Code: Select all

<?php
/* sendToHost
 * ~~~~~~~~~~
 * Params:
 *   $host      - Just the hostname.  No http:// or 
                  /path/to/file.html portions
 *   $method    - get or post, case-insensitive
 *   $path      - The /path/to/file.html part
 *   $data      - The query string, without initial question mark
 *   $useragent - If true, 'MSIE' will be sent as 
                  the User-Agent (optional)
 *
 * Examples:
 *   sendToHost('www.google.com','get','/search','q=php_imlib');
 *   sendToHost('www.example.com','post','/some_script.cgi',
 *              'param=First+Param&second=Second+param');
 */

function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 443);
    if ($method == 'GET') {
        $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
    fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent) {
        fputs($fp, "User-Agent: MSIE\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}
?>

Posted: Tue Jul 17, 2007 8:21 am
by volka
The code snippets you've posted do not generate any output, what do you expect?
I've noticed
superdez wrote:$fp = fsockopen($host, 443);
443 is the port for https but there's no TLS/SSL in your code.

Posted: Tue Jul 17, 2007 8:24 am
by superdez
Ok, so any ideas of how to modify this script so as to submit information towards WorldPay so people can pay ?

Posted: Tue Jul 17, 2007 8:27 am
by volka
You might be interested in http://de2.php.net/curl

Posted: Tue Jul 17, 2007 8:30 am
by superdez
Thank you for your input...

I got this other function but it does not redirect to google.com, it just stays at my site and the headers are printed on the page.

Why doesn't it redirect ? Why does it print the headers ?



Code: Select all

<?php
function PostToHost($host, $path, $data_to_send) {
$fp = fsockopen($host,80);
fputs($fp, "POST $path HTTP/1.0\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: ".strlen($data_to_send)."\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, $data_to_send);
while(!feof($fp)) { echo fgets($fp, 128);
} fclose($fp);
} 
PostToHost("www.google.com","/search",urlencode("cEmail=me@here.com&cMsg=The outlook wasn't brillant for the Mudville nine that day, the score stood two to four with but an inning left to play..."));
exit(); 
?>

Posted: Tue Jul 17, 2007 8:38 am
by volka
superdez wrote:Why doesn't it redirect ?
There is no redirect code in the script. If you want to redirect he client use header() or meta/refresh.
superdez wrote:Why does it print the headers ?
while(!feof($fp)) { echo fgets($fp, 128);
does that, it prints the raw response data it receives from the socket.