I'm having a bit of an issue with curl. It simply wont take $variables.. Kindly assist. The rest of the curl request is correct and I've managed to isolate it
Here is what I've tried
CURLOPT_POSTFIELDS => "{\"msisdn\":$msisdn, \"transactionId\": $_id, \"transactionAmount\": $amount}",
$payload = json_encode(array("msisdn"=> $s_msisdn,"transactionId"=>$_id, "transactionAmount"=>$_amount));
...
CURLOPT_POSTFIELDS => $payload,
EDIT: Full request
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"msisdn\":"'.$_msisdn.'", \"transactionId\": "'.$_id.'", \"transactionAmount\": "'.$_amount.'"}",
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
),
));
Here is what you can use. Your JSON string is not got the correct quotations. I personally find it easier to use an array then json_encode it, it saves having to worry about escaping variables.
$data = array (
"msisdn" => $_msisdn,
"transactionId" => $_id,
"transactionAmount" => $_amount,
);
$jsonData = json_encode($data);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
),
));
This is more closer to your code with the JSON fixed.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"msisdn\": \"{$_msisdn}\", \"transactionId\": \"{$_id}\", \"transactionAmount\": \"{$_amount}\"}",
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
)
));
Related
How to use variable on postfields json type on php ?
I set deger variable 123 but curl don't send this data please help me
<?php
$deger=123;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://serviceurl',
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 =>'{
"iprocessnumber": "$deger",
"activationStatus": "SUCCESS",
"code": "100",
"message": "Test mesajı"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
You could use that CURLOPT_POSTFIELDS to be pointed to a JSON encoded array.
$postFields holds the encoded fields.
<?php
$deger=123;
$curl = curl_init();
$postFields= [
"iprocessnumber"=>$deger,
"activationStatus"=>"SUCCESS",
"code"=>"100",
"message"=>"Test mesajı"
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://serviceurl',
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 =>json_encode($postFields),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Hope this helps.
I was able to find the access token from Azure
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://login.microsoftonline.com/12baf------------/oauth2/token',
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 => 'grant_type=client_credentials&client_id=XXXXX-57b4-XXXXb-XXXX-13ca1f5eec6e&client_secret=XXXXX&resource=https%3A%2F%2Fmanagement.azure.com%2F',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: fpc=AlXXXXXXXXpKm43KbQ52MOkk8jRRAQAAAI8Us9cOAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=prod'
),
));
$json = curl_exec($curl);
$data=json_decode($json,true);
curl_close($curl);
$token=$data['access_token'];
But when I am trying to pass the variable in CURLOPT_HTTPHEADER it's showing an invalid token.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://management.azure.com/subscriptions/XXXXXXX-1faf-4756-a709-1af49be58e56/resourcegroups?api-version=2020-06-01',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '$token'
),
));
$response = curl_exec($curl);
echo $response
?>
How can I pass the token in the above array or there is an easy way to do it? Can anyone please help here.
I able to resolve it as below
$authorization = "Authorization: Bearer ".$token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://management.azure.com/subscriptions/xxxxxxx-4756-a709-1af49be58e56/resourcegroups?api-version=2020-06-01',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json' , $authorization)
),
);
$response = curl_exec($curl);
echo $response
?>
I have a form that get the data values (uid,f_1_email, etc...) i need to send to an API, i need to get those values a put in this URL encoded.
"cid=199&sid=2&uid=[uid]&f_1_email=[f_1_email]&f_3_firstname=[f_3_firstname]&f_4_lastname=[f_4_lastname]&f_11_postcode=[f_11_postcode]&f_12_phone1=[f_12_phone1]&f_135_nombre_empresa=[f_135_nombre_empresa]&f_134_cantidad_vehiculos=[f_134_cantidad_vehiculos]&f_133_tipo_servicio=[f_133_tipo_servicio]
"
This is the full code
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://leadtowin.databowl.com/api/v1/lead",
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 => "cid=199&sid=2&uid=[uid]&optin_email=[optin_email]&optin_email_timestamp=[optin_email_timestamp]&optin_phone=[optin_phone]&optin_phone_timestamp=[optin_phone_timestamp]&optin_sms=[optin_sms]&optin_sms_timestamp=[optin_sms_timestamp]&optin_postal=[optin_postal]&optin_postal_timestamp=[optin_postal_timestamp]&f_1_email=[f_1_email]&f_3_firstname=[f_3_firstname]&f_4_lastname=[f_4_lastname]&f_11_postcode=[f_11_postcode]&f_12_phone1=[f_12_phone1]&f_135_nombre_empresa=[f_135_nombre_empresa]&f_134_cantidad_vehiculos=[f_134_cantidad_vehiculos]&f_133_tipo_servicio=[f_133_tipo_servicio]
",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: UTF-8",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: PHPSESSID=d053646c7953d9116849a8aa4717ab81"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I try this but it does not work
"cid=199&uid=$_POST['uid']&f_1_email=$_POST['email']&f_3_firstname=$_POST['firstname']&f_4_lastname=$_POST['lastname']&f_11_postcode=$_POST['meta_Zip']&f_12_phone1=$_POST['mobile_phone']&f_135_nombre_empresa=$_POST['meta_empresa']&f_134_cantidad_vehiculos=$_POST['meta_cantidadVehiculos']&f_133_tipo_servicio=$_POST['meta_Gestion']"
Maybe the native PHP functionality can easy this process for you ( https://www.php.net/manual/en/function.http-build-query.php ). Using an array also makes it a lot better readable, and thus cleaner and less error phrone:
<?php
$post_array = array(
"cid" => "199",
"sid" => "2",
"uid" => $_POST['uid'],
"optin_email" => $_POST['optin_email'],
"optin_email_timestamp" => $_POST['optin_email_timestamp'],
"optin_phone" => $_POST['optin_phone'],
"optin_phone_timestamp" => $_POST['optin_phone_timestamp'],
"optin_sms" => $_POST['optin_sms'],
"optin_sms_timestamp" => $_POST['optin_sms_timestamp'],
"optin_postal" => $_POST['optin_postal'],
"optin_postal_timestamp" => $_POST['optin_postal_timestamp'],
"f_1_email" => $_POST['f_1_email'],
"f_3_firstname" => $_POST['f_3_firstname'],
"f_4_lastname" => $_POST['f_4_lastname'],
"f_11_postcode" => $_POST['f_11_postcode'],
"f_12_phone1" => $_POST['f_12_phone1'],
"f_135_nombre_empresa" => $_POST['f_135_nombre_empresa'],
"f_134_cantidad_vehiculos" => $_POST['f_134_cantidad_vehiculos'],
"f_133_tipo_servicio" => $_POST['f_133_tipo_servicio']
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://leadtowin.databowl.com/api/v1/lead",
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 => http_build_query($post_array),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: UTF-8",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: PHPSESSID=d053646c7953d9116849a8aa4717ab81"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
This should do.
I am running a cURL request that looks like this,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
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(
"Content-Type: application/json",
"Authorization: Bearer " . $token
)
));
$response = curl_exec($curl);
die(var_dump($response));
Would there be any reason for the my cURL in PHP not returning a response?
I am a beginner with curl. I want to make a request where I post different data.
I have a code
$curl = curl_init();
$fields = (object) array(
'contactFilter' => (object) array(
'clicked_message.messageid' => '5',
'messages_sent.messageid' => '5'
),
'exportAttributes' => 'email',
);
$fields = json_encode($fields);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'api-key: my-key-12345',
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields_string
));
$response = curl_exec($curl);
Documentation says that I need;
contactFilter object (X= campaign id): {"clicked_message.messageid": X,"messages_sent.messageid": X}
and
exportAttributes` array of strings For example, `['fname', 'lname, 'email'].
how should my request should look like?
You need to use CURLOPT_POSTFIELDS to post the fields, for example
$fields = array(
'username' => "annonymous",
'api_key' => "1234"
);
$fields_string = http_build_query($fields);
Now use variable fields_string to send array data
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
Your curl request will be look like
curl_setopt_array($curl,
array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields_string
)
);
Updated Code-
$curl = curl_init();
$fields = (object) array(
'contactFilter' => (object) array(
'clicked_message.messageid' => '5',
'messages_sent.messageid' => '5'
),
'exportAttributes' => 'email',
);
$fields = json_encode($fields);
//$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'api-key: my-key-12345',
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields
));
$response = curl_exec($curl);
echo $response;
print_r($response);