xxxxxxxxxx
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, https://95.111.250.84:2304/v1/account);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt ($ch, CURLOPT_POST, 1);
$response = curl_exec($ch);
curl_close($ch);
xxxxxxxxxx
function getUrl($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
xxxxxxxxxx
<?php
// From URL to get webpage contents
$url = "https://www.geeksforgeeks.org/";
// Initialize a CURL session.
$ch = curl_init();
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Grab URL and pass it to the variable
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
echo $result;
?>
xxxxxxxxxx
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.example.com/yourscript.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
// result sent by the remote server is in $result
xxxxxxxxxx
It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format
<?php
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36)
$defaults = array(
CURLOPT_URL => 'http://myremoteservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
?>
This produce the following post header:
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="name"
Jhon
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="surnname"
Doe
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="age"
36
--------------------------fd1c4191862e3566--
Setting CURLOPT_POSTFIELDS as follow produce a standard post header
CURLOPT_POSTFIELDS => http_build_query($params),
Which is:
name=John&surname=Doe&age=36
This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem.
xxxxxxxxxx
<?php
$ch = curl_init();
$url = "https://example.com/api";
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer your_access_token'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
// Process the response
echo $response;
}
curl_close($ch);
?>
xxxxxxxxxx
$localFile = 'C:\SampleWithout-URL.json';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://globalshipmeds.com/values/Upload',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('' => new CurlFile($localFile)),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer evzPukXUMTiEtjk4s3gijNmG8LhGH7suo0Rk9vyoGXt6X15pRseSGxa829B0JcQKE0yp8CtY37pc37cEw_rF8GcgIa6iv_WKsvBBoAvAAShWkq_sJK_v99qjirUqP0qpZXjPTJNfuerxooGQhy8PS11ZBmtelOgByWAMJD_pABuGebmk1ASWepfFBAcPu9zjpo8jqD9XsitUmdSNtkbho2-Lr1kOubTA_lF9_9jdyJpL444KcLDUHi3OjEjcPXc_s-XVEK49KYjzcuK6YCUyeu5f1r0rW9GPV_3C4SfFqNt4nrdjRvhx_UhZvYsoGBFE'),
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);