How to emulate form action in php?

Discussion in 'PHP' started by cscott5288, Apr 25, 2010.

  1. #1
    Okay, here's the delio.

    I'm sending data to a php script using the post method, analyzing it and changing it there (with a yes/no boolean) and then sending it to another php script located off server.

    How do I emulate the post method within php so I can send it to the other server?

    That's pretty much about as simple as I can explain it. I would greatly appreciate any help!
     
    cscott5288, Apr 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Use $_GET, or cURL, to send data to another site.
     
    danx10, Apr 25, 2010 IP
  3. cscott5288

    cscott5288 Active Member

    Messages:
    912
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I understand how to use $_GET and $_POST to send data to a php file on another site from a form in HTML but I don't understand how to do it by executing a chunk of code in PHP.
     
    cscott5288, Apr 25, 2010 IP
  4. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You will need to use cURL to post the data offsite.
     
    Brad33, Apr 25, 2010 IP
  5. cscott5288

    cscott5288 Active Member

    Messages:
    912
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Okay, I am looking at the cURL docs and it is making absolutely no sense:
    http://php.net/manual/en/book.curl.php

    I am pretty new to php ...

    Does anyone have an example I can look at for the specific thing that I want to do?
     
    cscott5288, Apr 25, 2010 IP
  6. hugsbunny

    hugsbunny Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Search for a tutorial on cURL ,,, you don't have another option
     
    hugsbunny, Apr 26, 2010 IP
  7. alemcherry

    alemcherry Well-Known Member

    Messages:
    146
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    140
    #7
    alemcherry, Apr 26, 2010 IP
  8. cscott5288

    cscott5288 Active Member

    Messages:
    912
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Okay, the David Walsh script looks pretty easy. I only have a few questions:

    //extract data from the post
    extract($_POST);
    
    //set POST variables
    $url = 'http://domain.com/get-post.php';
    $fields = array(
    						'lname'=>urlencode($last_name),
    						'fname'=>urlencode($first_name),
    						'title'=>urlencode($title),
    						'company'=>urlencode($institution),
    						'age'=>urlencode($age),
    						'email'=>urlencode($email),
    						'phone'=>urlencode($phone)
    				);
    
    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    PHP:
    Am I correct in thinking the extract function allows you to put the Post data in an array which you must 'URL encode' to send to another URL with cURL?

    Can you explain what exactly is going on with 'URLifying' the post:

    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
    PHP:
    ?

    Also, I don't understand this part as well:

    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    PHP:
    Thanks, I appreciate the help ... I just started with PHP a few weeks ago.
     
    cscott5288, Apr 26, 2010 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    
    PHP:
    can be replaced with
    
    $fields_string = http_build_query($fields);
    
    PHP:
    Read about it here http://www.php.net/manual/en/function.http-build-query.php

    
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    
    PHP:
    When you initialize cURL (with $ch = curl_init();) you set its options using curl_setopt function, first parameter always being the cURL instance (in this case it's $ch), second param is option name and third param is option value.
    CURLOPT_URL is the URL you want to send your request to.
    CURLOPT_POST takes boolean value and it indicates weather you want it to be GET or POST request (defaults to GET).
    CURLOPT_POSTFIELDS is the HTML query string that you want to send with GET/POST method.
     
    Gray Fox, Apr 26, 2010 IP
  10. cscott5288

    cscott5288 Active Member

    Messages:
    912
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Okay I am just looking to send only one field of information as a string. Does that mean my code will look like this?

    //set POST variables
    $url = 'http://domain.com/get-post.php';
    $field = $_POST['Email'];
    
    //url-ify the data for the POST
    $field_string = http_build_query($field);
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($field));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$field_string);
    
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    PHP:
     
    cscott5288, Apr 27, 2010 IP
  11. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #11
    
    //set POST variables
    $url = 'http://domain.com/get-post.php';
    $field = array('email' => $_POST['Email']); //you have to enter POST parameter name for every post field
    
    //url-ify the data for the POST
    $field_string = http_build_query($field);
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$field_string);
    
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    
    PHP:
     
    Gray Fox, Apr 28, 2010 IP
  12. cscott5288

    cscott5288 Active Member

    Messages:
    912
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #12
    Why does the data have to be sent as an array?

    Thanks for the help again.
     
    cscott5288, Apr 28, 2010 IP
  13. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It is in an array because that's what the http_build_query function needs in order to turn it into a query string.
     
    Brad33, Apr 28, 2010 IP