Fopen Fwrite To Another Domain's Sub Directory

Discussion in 'PHP' started by jeffshead, Feb 3, 2013.

  1. #1
    Can some please tell me the correct, secure way to POST to a file on another domain?

    I have two domains. Actually, in this particular instance, I'm working with two subdomains that are hosted on the same server.

    - www.mysite.com
    - support.mysite.com

    I have a function on support.mysite.com that needs to POST to a file on www.mysite.com.
    The file being POSTed to is not in the root. The file is here: www.mysite.com/folder/file.php.

    Here's what I have so far but it does not work:
    $fp = fsockopen('127.0.0.1', 80);
     
    if ($fp !== false) {
     
        $vars = array(
        'time' => $time,
        'hash' => $hash,
        'name' => $Name,
        'email' => $email,
        'password' => $rand_password,
        );
        $content = http_build_query($vars);
     
        fwrite($fp, "POST /folder/file.php HTTP/1.1\r\n");
        fwrite($fp, "Host: www.mysite.com\r\n");
        fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
        fwrite($fp, "Content-Length: " . strlen($content) . "\r\n");
        fwrite($fp, "Connection: close\r\n");
        fwrite($fp, "\r\n");
        fwrite($fp, $content);
     
        fclose($fp);
    }
    PHP:
    I think the problem is line 14 and 15.

    I tested the code (it worked) by relocating 'file.php' to the root of 'www.mysite.com' and editing line 14 as below:
    fwrite($fp, "POST /file.php HTTP/1.1\r\n");
    PHP:
    ...but I need 'file.php' to be in a specific subdirectory (e.g., www.mysite.com/folder/file.php)
     
    Last edited: Feb 3, 2013
    jeffshead, Feb 3, 2013 IP
  2. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #2
    Why you wouldn't want to use curl?
     
    Sano000, Feb 3, 2013 IP
  3. jeffshead

    jeffshead Member

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    To be honest, I don't remember. This was something that I implemented a while back on another site. Now I'm trying to replicate what did on another site but this time 'file.php' is not in the root.

    I can't remember why I did not use cURL but there was a reason because I remember debating which one to use . It could have simply been the fact that I could not get it to work with cURL or that someone told me fsockopen is more efficient than cURL.

    What would the code look like in cURL?
     
    jeffshead, Feb 3, 2013 IP
  4. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #4
    It could be something like that
    <?php
    $vars = array(
        'time' => $time,
        'hash' => $hash,
        'name' => $Name,
        'email' => $email,
        'password' => $rand_password,
        );
     
    if( $curl = curl_init() ) {   
        curl_setopt($curl, CURLOPT_URL, 'www.mysite.com/folder/file.php');   
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $vars);
       
        $result = curl_exec($curl); 
        curl_close($curl);
        echo $result;
    }
    ?>
    PHP:
     
    Sano000, Feb 3, 2013 IP
  5. jeffshead

    jeffshead Member

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    Thanks Sano000.
    What should the URL be if my server's file structure is such and I do not want to go over the Internet:
    C:/wamp/websites/www.mysite.com/folder/file.php
    C:/wamp/websites/support.mysite.com
     
    jeffshead, Feb 3, 2013 IP
  6. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #6
    I think, You can' do so. Post request processed by webserver which has a chrooted environment.
     
    Sano000, Feb 3, 2013 IP