POST Multiple POST Values
//Setup the POST fields
$PostData = array(
'my_field1' => $my_field1,
'my_field1' => $my_field1
);
$PostData = http_build_query($PostData);
$Url = 'https://urltosendto.com';
$Response = '';
if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //Connect timeout in secs
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //Maximum time the request is allowed to take in secs
$Response = curl_exec($ch);
$HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($HttpCode !== 200)
die("Curl request failed, HTTP status code: $HttpCode");
//echo($Response);
if (strpos($Response, 'SUCCESS') !== False)
{
//It worked
//echo"[Success]";
}
}
POST a json based request
$PostData = array(
'SomeField1' => $MyVariable,
'SomeField1' => 1
);
$PostData = json_encode($PostData);
$Url = "https://myurl.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); //<<<You can add header fields in here. e.g.: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Token:$MyAcccessToken"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Follow any page redirects etc (optional)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //<<<Include this for the output of curl to go to the curl_exec() output below instead of the browser screen
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //Connect timeout in secs
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //Maximum time the request is allowed to take in secs
$CurlResult = curl_exec($ch);
$HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($HttpCode !== 200)
die("Curl request failed, HTTP status code: $HttpCode");
$DecodedJson = json_decode($CurlResult, True);
echo "CurlResult: ";
print_r($CurlResult);
echo "<br>DecodedJson: ";
print_r($DecodedJson);
if (!is_null($DecodedJson))
{
//Check for succcess
if ( (isset($DecodedJson['errorCode'])) && ($DecodedJson['errorCode'] == 0) )
{
}
}
POST a json block with authorisation
$PostData = array(
'SomeField1' => $MyVariable,
'SomeField1' => 1
);
$PostData = json_encode($PostData);
$Url = 'https://urltosendto.com';
$Username = 'myusername';
$Password = 'mypassword';
if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$Username:$Password"), 'Content-Type: application/json'));
$Response = curl_exec($ch);
$HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
if ($HttpCode !== 200)
die("Curl request failed, HTTP status code: $HttpCode");
//response_code":"SUCCESS"
$Response = json_decode($Response, TRUE);
$response_code = $Response['response_code'];
if ($response_code == "SUCCESS")
{
}
Add header fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//For multiple entires use this instead:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Token:$MyAcccessToken"));
POST with a file
$FilePath = "/var/www/vhosts/mydomain.com/httpdocs/wp-content/uploads/my_logo.png";
$Url = 'https://urltosendto.com';
$PostData = array(
'SomeField1' => $MyVariable,
'SomeField1' => 1
);
//Add the file to upload to PostData
$UploadFileName = basename($FilePath); //Get just the file name from the path
$MimeContentType = mime_content_type($FilePath); //Get the MIME type of the file
$PostData['file'] = new \CURLFile($FilePath, $MimeContentType, $UploadFileName); //Add the file to the PostData (Remove the leading '\' if not using within a namespace
//Do not use http_build_query() when doing 'multipart/form-data'
$Response = '';
if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //Connect timeout in secs
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //Maximum time the request is allowed to take in secs
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $PinataJwt", "Content-Type: multipart/form-data"));
$Response = curl_exec($ch);
$HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$DecodedJson = json_decode($Response, True);
echo "CurlResult: ";
print_r($Response);
echo "<br>DecodedJson: ";
print_r($DecodedJson);
if ($HttpCode !== 200)
die("Curl request failed, HTTP status code: $HttpCode");
