POST Multiple POST Values
//Setup the POST fields
$PostData = array(
'my_field1' => $my_field1,
'my_field1' => $my_field1
);
$PostData = http_build_query($PostData);
$SendUrl = '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, $SendUrl);
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);
curl_close($ch);
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);
curl_close($ch);
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);
$SendUrl = '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, $SendUrl);
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);
curl_close($ch);
}
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"));
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.