I have this function where I enroll a token to make a transaction process without the need to key-in of credentials.
Some blogs stated that curl exec does the job in executing the curl, but nothing is happening with this function. can I somehow trigger this CURL using submit button in form? also, how can I know the errors of CURL?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'url here',
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 =>'{
"clientIp": $_clientip,
"ipAddress": $_ipaddress,
"merchantId": $_mid,
"notificationUrl": $_noturl,
"requestId": $_requestid,
"responseUrl": $_resurl,
"signature": "1f4856d7868b1c352e015211e79df194d6fa1babc624084dd00027bf89538184d75f19991d008c1d99d2b5a28c2eea7f928e304611562eb838216e3664eac628",
"trxType": "createtoken",
"verify": "Y",
"address1": $_addr1,
"address2": $_addr2,
"city": $_city,
"country": $_country,
"email": $_email,
"fName": $_fname,
"lName": $_lname,
"mName": $_mname,
"mobile": $_mobile,
"phone": $_phone,
"state": $_state,
"postal": ""
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Idempotency-Key: HMCENRO202001140009T',
'Authorization: Basic Og=='
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
you can try it via defining the body as structured representation and then transform it to JSON:
$recipient['dst'] = "+000000";
$message['recipients'] = array($recipient);
$message['text'] = "this is a test message";
$job['messages'] = array($message);
$json = json_encode($job);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'url here',
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,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Idempotency-Key: HMCENRO202001140009T',
'Authorization: Basic Og=='
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Related
I'm using php curl API to fetch the posts from WordPress. But I'm not getting X-WP-Total or X-WP-TotalPages keys with response to work with pagination.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/posts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, OPTIONS",
"Access-Control-Allow-Headers: X-Requested-With"
],
));
$response = curl_exec($curl);
$response = json_decode($response);
// print_r($response);exit;
curl_close($curl);
I have a method that sends an SMS message but instead returns false. The code is used in Postman, and it works. I am using Laravel 5.8.
public function sendSMS()
{
$curl = curl_init();
curl_setopt_array(URL_API, array(
CURLOPT_URL => $url,
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 => '{
"number" : PHONE_NUMBER,
"message" : "Tu código de seguridad es: 289686",
"type" : 1
}',
CURLOPT_HTTPHEADER => array(
'api-key: API_KEY',
'Authorization: Bearer TOKEN_API',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
Response in Laravel
[false]
0: false
Response in Postman
{
"success": true,
"message": "Enviado",
"number": "11111111111",
"menssageId": 122585244
}
I verified that CURL is enabled on my localhost.
cURL support : enabled
cURL Information : 7.70.0
I thank you all for your time, the solution was added two parameters in the curl query.
public static function sendSMS($number,$message){
$token = json_decode(self::loginCELLVOZ(),true);
$url = env('URL_BASE') . 'sms/single';
$account = env('ACCOUNT');
$password = env('PASSWORD');
$api_key = env('API_KEY');
$phone = '551'.$number;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, //add line
CURLOPT_SSL_VERIFYHOST => false, //add line
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"number" : "'.$phone.'",
"message" : "'.$message.'",
"type" : 1
}',
CURLOPT_HTTPHEADER => array(
'api-key: '.$api_key.'',
'Authorization: Bearer '.$token['token'].'',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
NOTE:
Only is necesary this lines in localhost, not recomendly use in the server production.
I have a question about curl.
Here is the value which will be changed by the user input.
$uid = "xxxxx" //Get from system
$token = "xxxxx" //Get from system
$title = "Testing Message"; //user input
$body = "Message"; //user input
$url_on_click = "https://www.xxxxx.com"; //user input
I want to apply the value into the function but it getting error:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
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 =>'{
"uid" :' . $record_uid . ',
"payload": ' . $payload . '
}',
CURLOPT_HTTPHEADER => array(
'token: ' . $record_token . ',
Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
If I change all the value to string. It's work. The follow code like :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
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 =>'{"uid" :"xxxxxx",
"payload": {
"title": "Testing Message",
"body": "1234",
"icon":"https://xxxxx.net/xxxxx.png",
"urlOnClick":"xxxxx.com"
}}',
CURLOPT_HTTPHEADER => array(
'token: xxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
May I know which part I did wrong? That issue really pull my hair out.
Thank you =]
Try this, post fields depend on your CURLOPT_HTTPHEADER, according to your header you need to convert it in JSON otherwise, the array type will work
$postFields = array(
'uid' => "xxxx",
'token' => "xxxx",
'title' => "xxxx",
'body' => "xxxx",
'url_on_click' => "xxxx"
);
//curl init lines
//-------
//-------
CURLOPT_POSTFIELDS => json_encode($postFields),
How I can create a user by Client secrets in Azure AD with PHP?
I need access token in below code to create a user. To have this token I need to login first. How I can create a user automatically without any login.
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
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 =>'{
"accountEnabled": true,
"displayName": "Adele Vance",
"userPrincipalName": "adelev2#xxx.net",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
),
));
You can refer to this sample, which uses a daemon that does not require user login, and uses the client credential flow to obtain an access token to call MS graph api to create a user. You need to grant User.ReadWrite.All application permissions for the application.
With special thanks to Carl which provide useful links I did it by using two below functions:
I receive a token by calling getToken function and use it in getToken to create a user without any previous login.
function getToken() {
$curl = curl_init();
$dir = env('OAUTH_DIR_ID');
$clientId = env('OAUTH_APP_ID');
$secretKey = env('OAUTH_APP_PASSWORD');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/$dir/oauth2/v2.0/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 => "client_id=$clientId&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=$secretKey&grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'x-ms-gateway-slice=estsfd; stsservicecookie=estsfd'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
function addUser($accessToken)
{
try {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
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 =>'{
"accountEnabled": true,
"displayName": "Adele Vance",
"userPrincipalName": "adelev2#yoed.net",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response); // Debug print
exit();
} catch (Error $ex) {
$home = env('APP_URL');
header("Location: $home/signin.php?err=" . $ex->getMessage());
die();
}
}
I am able to generate access_token from production environment by given below code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.amadeus.com/v1/security/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "client_id=XXX&client_secret=XXX&grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
after that i am trying to call
https://api.amadeus.com/v1/shopping/flight-dates?origin=MAD&destination=MUC
API. By using following code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.amadeus.com/v1/shopping/flight-dates?origin=JFK&destination=LHR&oneWay=false&nonStop=false",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/x-www-form-urlencoded",
"Authorization: Bearer XXX",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
i am getting following error
{
"errors": [
{
"status": 401,
"title": "Wrong authentication credentials.",
"code": 701,
"source": {
"pointer": "shopping/flight-dates"
}
}
]
}
what am i doing wrong.
We had an internal configuration issue that has been fixed everything should work fine now.
2 things:
This API doesn't support airport codes you will need to replace them by city codes (LHR-> LON / JFK -> NYC), this is part of our backlog to support airport codes as well but not delivered yet. So API call should be:
https://api.amadeus.com/v1/shopping/flight-dates?origin=NYC&destination=LON&oneWay=false&nonStop=false
The only needed header is:
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer XXX",
),