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!
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.
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?
Basically you will have to use fsockopen to open a socket connection to the target server, communicate using the HTTP protocol with fwrite etc. using get or post. FsockOpen is the function you need to unserstand, but cURL is a library that makes this kind of communication much easier. There are tons of examples available on net. http://php.net/manual/en/function.fsockopen.php http://davidwalsh.name/execute-http-post-php-curl
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.
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.
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:
//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:
It is in an array because that's what the http_build_query function needs in order to turn it into a query string.