$my_curl = curl_init(); // Initializes a new session and returns a cURL handlecurl_setopt($my_curl, CURLOPT_URL, "https://www.google.com"); // Set the Optioncurl_exec($my_curl); // Retrieve data and showcurl_close($my_curl); // Close the handler
<?php
$my_curl = curl_init();
$str = "John"; // Data as query string parametercurl_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 stringcurl_close($my_curl); // Close the handlerecho$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 stringif (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 datacurl_close($my_curl); // Close the handlerecho$return_str; // Display the output
?>
Output is here
Welcoem Alex John
The script at remote server ( my_script.php )is here.
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 objectif (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 datacurl_close($my_curl); // Close the handlerecho$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 the same path as the script.$add = "upload/$file_name"; // Path to store uploaded fileif(move_uploaded_file($_FILES['file_up']['tmp_name'], $add)) {
echo"File uploaded";
} else {
echo"File Not uploaded";
}
?>
PageSpeed Insights (PSI)
PageSpeed Insights analyzes the content of a web page and generates suggestions to make it faster. This service evaluates web performance using both lab and field data to provide recommendations for improvement.
By leveraging both real-user experiences and simulated environments, PageSpeed Insights offers a comprehensive view of a page's performance. This helps developers optimize their sites, ensuring faster load times and a better user experience.
For more information, visit Google's PageSpeed Insights.
<?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 parametercurl_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
?>