SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fsocketopen() unable to connect

    Hi Guys,

    I am getting this error on one of my pages where I am trying to connect to Paypal:

    PHP Code:
    Warningfsockopen() [function.fsockopen]: unable to connect to http://www.paypal.com/cgi-bin/webscr:80 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in C:\wamp\www\Software\v2.5\classes\paypal.class.php on line 58 
    In php.ini I have:

    PHP Code:
    allow_url_fopen On 
    Any idea what may be causing this error?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //wrong
    fsockopen('http://www.paypal.com', ...);
    // right
    fsockopen('www.paypal.com', ...); 

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fsockopen() implements sockets at the transport layer. This includes TCP and UDP. HTTP is at the application layer, so fsockopen() doesn't understand what "http://" is. It will, however, understand "tcp://" and "udp://," although it defaults to TCP.

    See the docs for fsockopen() on using HTTP. It's one of the examples.

  4. #4
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I am now getting the following error after fixing the "http" problem:

    Code:
    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\software\v2.5\classes\paypal.class.php on line 58
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to www.paypal.com/cgi-bin/webscr:80 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in C:\wamp\www\software\v2.5\classes\paypal.class.php on line 58

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any ideas guys?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For POST:

    Pick one.

    PHP Code:
    <?php
    $postdata 
    = array(
        
    'var1' => 'value1',
        
    'var2' => 'value2',
    );

    $fp = @fsockopen("www.paypal.com"80$errno$errstr5);

    if (!
    $fp) {
        echo 
    "Error: $errstr ($errno)";
    } else {
        
    $data http_build_query($postdata'''&');
        
        
    $out "POST /cgi-bin/webscr HTTP/1.0\r\n";
        
    $out .= "Host: www.paypal.com\r\n";
        
    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        
    $out .= "Content-Length: ".strlen($data)."\r\n";
        
    $out .= "Connection: Close\r\n\r\n";
        
    $out .= $data;
        
        
    fwrite($fp$out);

        
    $in_headers true;
        
    $headers '';
        
    $result '';
        while (!
    feof($fp)) {
            if (
    $in_headers) {
                
    $in fgets($fp1024);
                if (
    trim($in) == "") {
                    
    $in_headers false;
                } else {
                    
    $headers .= $in;
                }
            } else {
                
    $result .= fread($fp1024);
            }
        }
        
        
    fclose($fp);

        return 
    $result;
    }
    PHP Code:
    <?php
    $postdata 
    = array(
        
    'var1' => 'value1',
        
    'var2' => 'value2',
    );

    $opts = array('http' =>
        array(
            
    'method'  => 'POST',
            
    'header'  => 'Content-type: application/x-www-form-urlencoded',
            
    'content' => http_build_query($postdata'''&'),
            
    'timeout' => 5,
        )
    );
    $context stream_context_create($opts);

    // Fetch
    if (!$result = @file_get_contents("http://www.paypal.com/cgi-bin/webscr"false$context)) {
        echo 
    "Error";
    }

    return 
    $result;
    You can also use cURL, but I've never used cURL and so I can't help you. There are also other ways to do it, but the ones I posted (namely the latter one) are the simplest and most portable.

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    For POST:

    Pick one.

    PHP Code:
    <?php
    $postdata 
    = array(
        
    'var1' => 'value1',
        
    'var2' => 'value2',
    );

    $fp = @fsockopen("www.paypal.com"80$errno$errstr5);

    if (!
    $fp) {
        echo 
    "Error: $errstr ($errno)";
    } else {
        
    $data http_build_query($postdata'''&');
        
        
    $out "POST /cgi-bin/webscr HTTP/1.0\r\n";
        
    $out .= "Host: www.paypal.com\r\n";
        
    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        
    $out .= "Content-Length: ".strlen($data)."\r\n";
        
    $out .= "Connection: Close\r\n\r\n";
        
    $out .= $data;
        
        
    fwrite($fp$out);

        
    $in_headers true;
        
    $headers '';
        
    $result '';
        while (!
    feof($fp)) {
            if (
    $in_headers) {
                
    $in fgets($fp1024);
                if (
    trim($in) == "") {
                    
    $in_headers false;
                } else {
                    
    $headers .= $in;
                }
            } else {
                
    $result .= fread($fp1024);
            }
        }
        
        
    fclose($fp);

        return 
    $result;
    }
    PHP Code:
    <?php
    $postdata 
    = array(
        
    'var1' => 'value1',
        
    'var2' => 'value2',
    );

    $opts = array('http' =>
        array(
            
    'method'  => 'POST',
            
    'header'  => 'Content-type: application/x-www-form-urlencoded',
            
    'content' => http_build_query($postdata'''&'),
            
    'timeout' => 5,
        )
    );
    $context stream_context_create($opts);

    // Fetch
    if (!$result = @file_get_contents("http://www.paypal.com/cgi-bin/webscr"false$context)) {
        echo 
    "Error";
    }

    return 
    $result;
    You can also use cURL, but I've never used cURL and so I can't help you. There are also other ways to do it, but the ones I posted (namely the latter one) are the simplest and most portable.
    Thank you very much for your reply, but I would prefer it if I was able to get fsockopen() working as I am trying to integrate a Paypal IPN script that I have downloaded directly from the Paypal website.

    Do you have any idea what may be causing the error? Is there a setting in php.ini that could be causing this error?

    Thanks.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first code I wrote uses fsockopen. You need all that code to implement the bare minimum of the HTTP protocol.

    HTTP is at the "application" layer.
    TCP is at the "transport" layer.
    IP is at the "network" layer.
    Below that is the "link" layer. (Where Ethernet, ATM, frame relay, etc. resides.)

    A HTTP packet "fits" into a TCP packet. A TCP packet "fits" into an IP packet. Etcetera...

    The IP layer contains the IP address of the computer you want to talk to. When you put "www.paypal.com," it's at the IP level of communication. At the TCP layer, you specify the port. In this case, it's port 80. Then, at the HTTP level, you request the URI ("/cgi-bin/webscr" in this case).



    fsockopen() speaks at the transport layer. It can talk in TCP and IP, but it doesn't handle HTTP. My first snippet of code in my post implemented the minimum necessary to talk HTTP.

    If you want to avoid having to handle HTTP manually, you have to use a library OR make use of PHP's streams/protocol wrappers. My latter code snippet used PHP's streams/protocol wrappers.

    ----

    Edit:

    Here's the example from PayPal. Since it uses fsockopen(), it has to implement the bare minimum of the HTTP protocol.

    PHP Code:
    // read the post from PayPal system and add 'cmd'
    $req 'cmd=_notify-validate';

    foreach (
    $_POST as $key => $value) {
    $value urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }

    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " strlen($req) . "\r\n\r\n";
    $fp fsockopen ('ssl://www.paypal.com'443$errno$errstr30);

    // assign posted variables to local variables
    $item_name $_POST['item_name'];
    $item_number $_POST['item_number'];
    $payment_status $_POST['payment_status'];
    $payment_amount $_POST['mc_gross'];
    $payment_currency $_POST['mc_currency'];
    $txn_id $_POST['txn_id'];
    $receiver_email $_POST['receiver_email'];
    $payer_email $_POST['payer_email'];

    if (!
    $fp) {
    // HTTP ERROR
    } else {
    fputs ($fp$header $req);
    while (!
    feof($fp)) {
    $res fgets ($fp1024);
    if (
    strcmp ($res"VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
    }
    else if (
    strcmp ($res"INVALID") == 0) {
    // log for manual investigation
    }
    }
    fclose ($fp);
    }
    ?> 
    The ssl:// tells fsockopen() to use TCP wrapped in SSL.

    Supported transports: http://php.net/manual/en/transports.php
    Support streams: http://php.net/manual/en/wrappers.php
    (two different things)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •