Apply value into cURL on PHP - php

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),

Related

When I use a CuRL request in Laravel it returns false

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.

Payment plugin for Documents Seller

I try to build payment plugin fo Joomdonation Documents Seller, i can send order to Bank, pay and after have some problem, when order is created and sent, it has status: in_progress, after payment status changes to success, i try to change record in database with if ($status == success) but can not. here is both files, 1. i start payment
session_start();
defined('_JEXEC') or die;
class os_bog extends DSFPayment
{
public function __construct($params, $config = [])
{
parent::__construct($params, $config);
}
public function processPayment($row, $data)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ipay.ge/opay/api/v1/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',
CURLOPT_HTTPHEADER =>array('Content-Type: application/x-www-form-urlencoded','Authorization: Basic '. base64_encode($client_id.':'.$secret_key)),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
$token = $response ['access_token'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ipay.ge/opay/api/v1/checkout/orders',
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 =>'{
"intent": "CAPTURE",
"items": [
{
"amount": "'. $data['amount'].'" ,
"description": "'.$data['item_name'].'",
"product_id": "'.$data['item_name'].'"
}
],
"locale": "ka",
"redirect_url": "https://editor.ge/",
"shop_order_id": "'. $data['order_number'].'",
"show_shop_order_id_on_extract": false ,
"capture_method": "AUTOMATIC",
"purchase_units": [
{
"amount": {
"currency_code": "GEL",
"value": "'. $data['amount'].'"
}
}
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer' . $token ),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
$payment_hash = $response ['payment_hash'];
$order_id = $response ['order_id'];
if (isset($response['order_id'])) {
$payment_hash = $response['payment_hash'];
$order_id = $response['order_id'];
$redirect_url = $response['links'][1]['href'];
header("Location:" . $redirect_url);
}
$_SESSION['order_data'] = array(
'order_id' => $response ['order_id'],
'payment_hash' => $response ['payment_hash'],
);
return $response;
}
public function verifyPayment()
{
if ($this->validate())
{
}
}
protected function validate()
{
$this->notificationData = $_REQUEST;
return true;
}
}
i check status of payment
session_start();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ipay.ge/opay/api/v1/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',
CURLOPT_HTTPHEADER =>array('Content-Type: application/x-www-form-urlencoded','Authorization: Basic '. base64_encode($client_id.':'.$secret_key)),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
$token = $response ['access_token'];
$order_data = ($_SESSION['order_data']);
$order_id = $order_data['order_id'];
$payment_hash = $order_data['payment_hash'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ipay.ge/opay/api/v1/checkout/payment/'.$order_id,
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: Bearer' . $token ),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
p.s. i can not check payment inside plugin(first code), callback url is predefined by Bank and i was no able to put code inside "public function verifyPayment()"
I am far not experienced , could someone help? Thank you in advance

Not Able to Get Value Variable into Curl Php

I am trying to pass the Variable into Curl But It's Not Working as I am trying to Call JSON API I Tried all The Possible Ways But Ntg Worked.
$name = "SAI";
$amount = "42000";
$user = "1643031393";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.interakt.ai/v1/public/track/events/",
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 =>"{\"userId\":\"1643031393\",\"event\":\"SEVA BILL GENRATED\",\"traits\": {\"donated\":\"YES\",\"seva_name\":\"$name\",\"seva_amount\":\"$amount\",\"Seva Date\":\"12-22-2022\",\"E-Receipt URL\":\"https://ack.saaa.org"},\"createdAt\": \"2022-01-24T13:26:52.926Z\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic gfdsds"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;````
The value of CURLOPT_POSTFIELD option must be JSON format!
Like this:
$data = json_encode(array(
"name" => "Steve",
"amount" => 12345,
"user" => 123456789
));
$curl = curl_init();
curl_setopt_array($curl, array(
...
CURLOPT_POSTFIELDS => $data,
...
));

Sending JSON Post Request using CURL on Form Submit PHP

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;

Getting Data from API - PHP

I have been trying to connect to an API, which requires both a user and password key. I was able to connect using curl from the command line, however am having trouble using PHP.
I have tried using "user:password" as the AUTH key (which worked in curl), previously the code simply required a password and worked.
Below is the code that I am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "api-link-removed.com" . urlencode($location),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Basic " . AUTH_KEY,
"content-type: application/json"
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
Any suggestions to what I might be doing wrong would be much appreciated!
Thanks in advance!
Maybe Guzzle HTTP
$client = new GuzzleHttp\Client();
$response = $client->get('http://www.server.com/endpoint', [
'auth' => [
'username',
'password'
]
]);
https://docs.guzzlephp.org/en/stable/
I tried this:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt_array($curl, [
CURLOPT_URL => "your api url here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Name of api host: the name here",
"Api key : api key here"
]
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Categories