$my_curl = curl_init(); // Initializes a new session and returns 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
Sending data through query string and receiving data as string and displaying.
<?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";
}
?>
<?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";
}
?>
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.// Create one CURLFile object
if (function_exists('curl_file_create')) { // PHP 5.5+
$cFile = curl_file_create($path, 'image/jpeg');
} else {
$cFile = '@' . realpath($path);
}
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.
CURLOPT_POSTFIELDS => array(
'file_up' => $cFile
)
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. $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
<?php
$file_name = $_FILES['file_up']['name'];
// Create upload directory on the same path as 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";
}
?>
<?php
$key = 'YOUR_API_KEY'; # Your API Key
$url = 'https://www.plus2net.com/';
$x = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' . $url . '&key=' . $key . '&strategy=mobile';
$my_curl = curl_init();
$str = "John"; // Data as query string parameter
curl_setopt($my_curl, CURLOPT_URL, $x);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1); // Return as String
$json_data = json_decode(curl_exec($my_curl));
$data = $json_data->lighthouseResult->categories->performance->score;
echo $data;
curl_close($my_curl); // Close the handler
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.