PHP - cURL - POST Or GET Data on server
PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.
1)a simple POST image data and JSON code
cURL POST video data to server endpoint use authorization Basic with JSON data
Note Image data is base64
PHP POST Image with JSON data By cURL
<?php
if(isset($_POST["postData"]))
{
$ImagesData = $_POST["postImageData"]; // Get Image Data (Base64)
$userData = json_decode($_POST["postData"]);
$EventId = $userData->EventId;
$UserId = $userData->UserId;
$MimeType = $userData->Set[0]->MimeType;
$GroupID = $userData->Set[0]->GroupID;
$headerData = array(
"authorization: Basic The_Password==",
"cache-control: no-cache",
"content-type: application/json;"
);
$ImageData = '
{
"EventId": "'.$EventId.'",
"UserId": "'.$UserId.'",
"Image": '.$ImagesData.'
"Set": [
{
"MimeType": "'.$MimeType.'",
"GroupID": "'.$GroupID.'",
}
]
}';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://Target_EndPoint/');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $ImageData);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerData);
$response = curl_exec($curl);
$errNo = curl_errno($curl);
$errStr = curl_error($curl);
curl_close($curl);
echo $response;
return;
}
?>
2)POST Video to other server code
cURL POST video data to server endpoint with authorization Bearer and CURLFile
PHP POST Video Data Blob By cURL with CURLFile
<?php
// When receive POST data
if(isset($_POST["postData"]))
{
$uploadDir = 'uploadsFolder/';
$uploadFile = $uploadDir . basename($_FILES['video']['name']);
//Save the Video on Server
if (move_uploaded_file($_FILES['video']['tmp_name'], $uploadFile))
{
// Header part
$headerData = array(
"authorization: Bearer LoginNameAndPasswordCode",
"cache-control: no-cache",
"content-type: multipart/form-data;"
);
// Post Video
$videoData = array(
'name' => 'video',
'filename' => $_FILES['video']['name'],
'video' => new CURLFile($uploadFile, $_FILES['video']['type'], $uploadFile)
);
// Execute remote upload
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://Target_End_Point');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $videoData);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerData);
$response = curl_exec($curl);
$errNo = curl_errno($curl);
$errStr = curl_error($curl);
curl_close($curl);
echo "</br> error_Code :: ".$errNo;
echo "</br> error_Msg :: ".$errStr;
echo '</br> server_response :: '.$response;
// unlink($uploadFile); //Delete upload file if you don't need it keep on the server or don't save it on begin
return;
}
else
{
echo "upload error!\n";
return;
}
}
echo "Post error";
return;
?>
3)GET Data on Server endpoint code
cURL Get JSON data on Server endpoint
PHP GET JSON data By cURL
<?php
if(isset($_POST["GetData"]))
{
$GetData = json_decode($_POST["GetData"]);
$ClientId = $GetData->ClientId;
$Record = $GetData->Record;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://Endpoint_Target/".$ClientId."/TheEvents/".$Record,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic The_Password==",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
echo $response;
return;
}
echo "Request Error\n";
return;
?>