cURL Examples in PHP

  1. Reading URL
  2. Posting and receiving Data using query string.
  3. Posting and receiving Data using POST method.
  4. Uploading file .

Reading URL using cURL

$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
Sending data through query string and receiving data as string and displaying.
About Query string and GET Method of passing data.
<?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()
// 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.
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

uploadck.php

This file will receive the data and upload the file. Here the full code of this is available.
Copy or Download the source code for uploadck.php file from here.
OR, use this minimum code to place the uploaded file inside the upload directory.
<?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 ";
}
?>
Copying all files from source directory to remote server using cURL
Opening a URL by file_get_contents()
PHP Scripts

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer