Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2007
    Posts
    2

    Unanswered: passing POST variables "externally"

    Hi,
    i want to run a script that runs a query on a specific url and mines the data that the query returns. Problem is that there is a form there that uses the POST method. If was GET i would just do:

    $data = file_get_contents($url);

    and add the variables to the URL. any idea how i can pass the vars with POST?

    thanks.

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Look into Curl, which is part of the standard php build now I understand. You can use it to post to a remote form and retrieve the output that is returned.
    As an example :

    Code:
    <?php
    // Set the URL.
    $url = "http://www.somewebsite.com/form.php";
    
    // initialize curl handle
    $ch = curl_init();
    
    // set url to post to
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    
    // allow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // return into a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
    // set POST method
    curl_setopt($ch, CURLOPT_POST, 1); 
    // add POST fields
    curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=blah&var2=blahblah");
    
    // Execute curl and send the url (<= a rhyme) 
    // and put the returned content into a variable.
    $result = curl_exec($ch);
    
    // Close the connection
    curl_close($ch); 
    
    // Print out the result.
    echo $result;
    ?>

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Or of course you could use streams instead....
    There's a good example on the php site http://uk.php.net/stream
    of which I shall include the code here:
    Code:
    <?php
    /* Send POST request to https://secure.example.com/form_action.php
     * Include form elements named "foo" and "bar" with dummy values
     */
    
    $sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
    if (!$sock) die("$errstr ($errno)\n");
    
    $data = "foo=" . urlencode("Value for Foo") . "&bar=" . urlencode("Value for Bar");
    
    fwrite($sock, "POST /form_action.php HTTP/1.0\r\n");
    fwrite($sock, "Host: secure.example.com\r\n");
    fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
    fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
    fwrite($sock, "Accept: */*\r\n");
    fwrite($sock, "\r\n");
    fwrite($sock, "$data\r\n");
    fwrite($sock, "\r\n");
    
    $headers = "";
    while ($str = trim(fgets($sock, 4096)))
      $headers .= "$str\n";
    
    echo "\n";
    
    $body = "";
    while (!feof($sock))
      $body .= fgets($sock, 4096);
    
    fclose($sock);
    ?>

  4. #4
    Join Date
    Sep 2007
    Posts
    2
    thanks aschk!

    i'll give both a try, hopefully at least one will owrk the way i want it to.

Posting Permissions

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