How to make a GuzzleHttp post request with body and header? - php

I'm not able to understand how to modify this cURL into Laravel 5.8, getting {"code":"11", "message":"invalid Request found", "status":"DECLINED"} response.
Here is my cURL code which is working fine (in postman and browser):
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
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 => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Here is my laravel code:
public function process(Request $request)
{
// return $request['request'];
$client = new \GuzzleHttp\Client();
$url = "https://api.hylo.biz/Api/v1.0/Payment";
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => 'Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type' => 'application/json'
],
'form_params' => $request['request']
]);
return $response = $response->getBody();
}

in this way it's working
public function process(Request $request)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
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 => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
}
and also I forgot to add use Illuminate\Http\Request;

Related

How to get WordPress posts count using PHP Curl

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

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

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;
}

How to receive posted array data from model to resource API controller using CURL in Laravel?

I'm building API for Laravel and I want to send array of data using CURL from model to resource API controller.
Model
$curl = curl_init();
$headers = 'X-CSRF-Token:'.$token;
curl_setopt_array($curl, array(
CURLOPT_URL => $path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $action,
CURLOPT_POSTFIELDS => json_encode($course_data),
//CURLOPT_POSTFIELDS =>$course_data,
CURLOPT_HTTPHEADER=> $headers,
// CURLOPT_HTTPHEADER => array(
// // 'Accept: application/json',
// 'headers' => $headers
// ),
));
API Controller
public function store(Request $request)
{
$courses = Course::insert($request['course_data']);
return CourseResource::collection($courses);
// dd($action);
}

Categories