Post Form submission from PHP

Discussion in 'MySQL' started by Rahul Bose, Apr 13, 2011.

  1. #1
    hi, I was working on a project and can't seem to find any help on the web. I need to connect to a server with POST and use the returned data in the script. The server is HTTPS also.

    I've read that cURL should be able to do it, but everything I've tried won't work.

    Any help would be greatly appreciated.
     
    Rahul Bose, Apr 13, 2011 IP
  2. codeartist

    codeartist Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    your problem is not not much clear what you are really trying to do can you please post your script so that we can test them ?
     
    codeartist, Apr 18, 2011 IP
  3. x319

    x319 Well-Known Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Create a simple host file to call with the information.

    i.e:

    
    // host.php
    if (isset($_POST['conn'])) {
        $host = $_POST['host'];
        $user = $_POST['user'];
        $pass = $_POST['pass'];
        $db = $_POST['db'];
        // run few simple checks and mysql connect
        $return = 'return data'; // return whatever data you want.
        die($return);
    }
    
    // client.php
    if (true) { // check if form is submitted
        $fields = array(
            'host' => 'localhost',
            'user' => 'db_user',
            'pass' => 'pass',
            'db' => 'db_name'
        );
        $fstr = '';
        foreach($fields as $k=>$v) $fstr .= $k . '=' . $v . '&';
        rtrim($fstr, '&');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://location.to/host.php');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fstr);
        $result = curl_exec($ch); // get the return value from the host file
        curl_close($ch);
    
        var_dump($result);
    }
    
    PHP:
    If I understood you right, the above is a simple example of what you should be looking at.

    ** Oops... Realized this is a pretty old thread, but will probably help someone in the future..
     
    Last edited: Apr 21, 2011
    x319, Apr 21, 2011 IP