$my_curl = curl_init(); //Initializes a new session and return a cURL handle
curl_setopt($my_curl, CURLOPT_URL, "https://www.google.com"); // Set the Option
curl_exec($my_curl); // retrieve data and show
curl_close($my_curl);// Close the handler
<?Php
$my_curl = curl_init();
$str="John"; // data as query string parameter
curl_setopt($my_curl, CURLOPT_URL,"https://www.plus2net.com/php_tutorial/curl-test.php?str=$str");
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1); // Return as String
$return_str = curl_exec($my_curl); // Execute and return as string
curl_close($my_curl); // Close the handler
echo $return_str; // Show the string received from URL
?>
output
Welcome John
Data is submitted to the page curl-test.php at plus2net.com server. Here the following code is kept to receive and return the data.
<?Php
@$str=$_GET['str']; // collect data from query string
if(strlen($str) > 0 ){
echo "Welcome $str";
}else{
echo "No data received ";
}
?>
Using POST method & array
We will pass one array of option values and use the POST method to send data to remote server. The response is displayed.
<?Php
$my_curl = curl_init(); //new cURL handler
$my_array=array(
CURLOPT_URL =>'https://www.example.com/my_script.php',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTFIELDS => array(
'f_name' => 'Alex',
'l_name' => 'John',
)
);
curl_setopt_array($my_curl, $my_array); // use the array
$return_str= curl_exec($my_curl); // Execute and get data
curl_close($my_curl); // close the handler
echo $return_str; // Display the output
?>
Output is here
Welcoem Alex John
The script at remote server ( my_script.php )is here.
<?Php
@$f_name=$_POST['f_name'];
@$l_name=$_POST['l_name'];
if(strlen($f_name) > 0 ){
echo "Welcome $f_name $l_name";
}else{
echo "No data received ";
}
?>
Uploading file using CURL
Syntax for creating CURLFile object.
curl_file_create(path,mime_type,posted_filename)
path : Full path of the file to be uploaded. mime_type:(Optional) File MIME type. posted_filename : (Optional) file name of uploaded file.
Our posted data must match with the accepting script while uploading the file. Here we have used the same location ( script ) as given in this basic file upload script with the source code to download.
We are creating the CURLFile object by using curl_file_create()
In our upload script we have the file name parameter as file_up, so we will create the array using this name file_up as key and using the curl file object $cFile as value.
In our file upload script we are sending the file only without any other data. However other data can be included by expanding the array with other key value pairs.
Here is the full code.
$my_curl = curl_init(); //new cURL handler
$path='D:\\top2.JPG'; // Path of the file to upload
// create one CURLFile object
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($path,'image/jpeg');
} else { //
$cFile = '@' . realpath($path);
}
$my_array=array(
CURLOPT_URL =>'http://localhost/plus2_upload_v1/uploadck.php',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTFIELDS => array(
'file_up' => $cFile
)
);
curl_setopt_array($my_curl, $my_array); // use the array
$return_str= curl_exec($my_curl); // Execute and get data
curl_close($my_curl); // close the handler
echo $return_str; // Display the output
Below code will allow all type of files of any size. This can be a security issue. Use carefully for testing in local environment and don’t expose uploading code to outside to upload malicious files.
<?Php
$file_name=$_FILES['file_up']['name'];
// Create upload directory on same path of the script.
$add="upload/$file_name"; // path to store uploaded file
if(move_uploaded_file ($_FILES['file_up']['tmp_name'], $add)){
echo " File uploaded ";
}else{
echo " File Not uploaded ";
}
?>