While PHP website development you might come to situations where you need to post data without form either on local or remote server. To solve this problem you can either use PHP CURL library of functions or fsockopen() and fputs() functions.
CURL Example
It stands for “Client URL", and can be used to connect through a wide -range of protocols, for example, HTTP, FTP, telnet and others.
Initialize a CURL Function
$curl_connection = curl_init('http://www.domainname.com/target_url.php');
Making Array of Data
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
Preparing Data to Post
foreach ( $post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
Sending Data
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
Executing and Closing Connection
$result = curl_exec($curl_connection);
curl_close($curl_connection);
fsockopen and fputs Example
Making Array of Data
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
Preparing Data to Post
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
Adding a question mark at the beginning of the string
$post_string = '?' . $post_string;
Length of the data string
$data_length = strlen($post_string);
Opening Connection
$connection = fsockopen('www.domainname.com', 80);
Sending Data
fputs($connection, "POST /target_url.php HTTP/1.1rn");
fputs($connection, "Host: www.domainname.com rn");
fputs($connection,
"Content-Type: application/x-www-form-urlencodedrn");
fputs($connection, "Content-Length: $data_lengthrn");
fputs($connection, "Connection: closernrn");
fputs($connection, $post_string);
closing connection
fclose($fp);
Mabstraus wrote:
necesidad de comprobar:)