Results 1 to 4 of 4

Thread: php socket send receive like curl

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default php socket send receive like curl

    can anyone help me better understand socket connections please
    if i have a socket connection like so
    Code:
    $address = gethostbyname('**.***.**.***');
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $result = socket_bind($socket, $address, '8000');
    How can i have a particular file read this socket and how would i be able to pass strings or variables?
    The concept i have is like cUrl.
    In the example below i have the receive as receive.php
    with $_GET variables

    send:
    Code:
    $url = "http://mydomain.com/process.php?var=1";
    	$ch = curl_init($url);
    	curl_exec ($ch);
    	curl_close ($ch);
    receive:
    Code:
    $var= $_GET['var'];

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Are you on the sending or receiving end?
    Is there some reason you don't want to use cURL? You could also use fsockopen() to do this kind of thing; I find it much easier, personally.

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i wanted to set up both send and receive, this is to communicate with a different server on AWS.
    would fsockopen() work the same way as tcp, when communicating with other servers?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    yeah, you literally write the HTTP request and then send it to the target page. PayPal does this with some of their APIs.

    The receiving page sends the response as normal (no fancy whatevers, just echo it).

    example:
    PHP Code:
    <?php

    # untested, but similar to working scripts I've made in the past

    $connection fsockopen("example.com"80$errno$errstr30);
    if (!
    $connection) { print "$errstr ($errno)<br />\n"; }
    else {
        
    $request "GET /process.php?var=somevalue HTTP/1.1\r\n";
        
    $request .= "Host: example.com\r\n\r\n";
        
    fputs$connection,$request );
        while( !
    feof$connection ) ){
            print 
    fgets($fp128);
        }
        
    fclose($fp);
    }
    Last edited by traq; 10-16-2012 at 01:58 AM. Reason: corrected code: sorry, I was thinking of a POST request

Similar Threads

  1. socket programming
    By aqeel in forum PHP
    Replies: 0
    Last Post: 12-27-2011, 11:56 AM
  2. I/O Error: Socket closed in CSS
    By sparklingsky in forum CSS
    Replies: 2
    Last Post: 10-18-2009, 01:54 PM
  3. Opening socket to a file
    By techno_race in forum PHP
    Replies: 3
    Last Post: 09-21-2008, 01:54 AM
  4. socket error
    By dladenhe in forum MySQL and other databases
    Replies: 0
    Last Post: 06-23-2008, 04:35 AM
  5. Programing for send/receive fax
    By jackavin in forum PHP
    Replies: 1
    Last Post: 09-13-2007, 03:29 PM

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
  •