php patch method curl request send - php

I need below code to php curl request to remote server. i am not able to do this and not getting source from google
curl -X PATCH
--header "Content-Type: application/json"
--header "Accept: application/json"
--header "x-api-token: API_TOKEN"
--header "x-api-user: API_USER" --data '{"profile_id":"string","msg_product":["A2P","P2P"]}'
"https://api.telnyx.com/messaging/numbers/{tn}"

Some php equivalent could be :
using curl :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.telnyx.com/messaging/numbers/'.$tn,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS =>'{"profile_id":"string","msg_product":["A2P","P2P"]}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'x-api-token: API_TOKEN',
'x-api-user: API_USER'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
or using guzzle :
<?php
$client = new Client();
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'x-api-token' => 'API_TOKEN',
'x-api-user' => 'API_USER'
];
$body = '{
"profile_id": "string",
"msg_product": [
"A2P",
"P2P"
]
}';
$request = new Request('PATCH', 'https://api.telnyx.com/messaging/numbers/'.$tn, $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();

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

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

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;

How to write cURL request with basic auth in Laravel 8

I have the following code from PayPal Payout SDK to get access tokens from the PayPal API.
curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "CLIENT_ID:SECRET" \
-d "grant_type=client_credentials"
To get the access token, I have tried the following.
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = ['grant_type:client_credentials'];
$response = Http::withHeaders([
'Accept:application/json',
'Accept-Language:en_US',
"Content-Type: application/x-www-form-urlencoded"
])->withBasicAuth($client_id, $secret)
->post($url, $data);
// OR
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization ' => ' Basic ' .
base64_encode($client_id . ':' . $secret)
],
'form_params' => [
'grant_type' => 'client_credentials',
]
]);
laravel 7 or 8 solution :
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = [
'grant_type' => 'client_credentials',
];
$response = Http::asForm()
->withBasicAuth($client_id, $secret)
->post($url, $data);
php native solution :
$client_id = "AWN5555";
$secret = "44444";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-m.sandbox.paypal.com/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 => [
'Authorization: Basic '.base64_encode($client_id.':'.$secret)
],
]);
$response = curl_exec($curl);
curl_close($curl);

getting auth error while calling search date api using production environment

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

Sending HTTP Post Request to site with array in Body

I'm trying to make a POST request and send some values in the body of an API call. In the documentation of the API it says I need to make a POST request, using startUrls as an array with key and value.
<?php
$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID';
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'body' => json_encode($postData)
)
));
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
?>
The JSON seems to be how it should, but the script is not sending the body properly to the website.
According to the documentation, there is no body option for the HTTP context. Try content instead:
<?php
$url = "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID";
$postData = [
"startUrls" => [
["key"=>"START", "value" => "https://instagram.com/instagram"]
]
];
$context = stream_context_create([
"http" => [
"method" => "POST",
"header" => "Content-type: application/json",
"content" => json_encode($postData)
]
]);
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
The following code will work. I have set the headers and specified the content type.
$request = new HttpRequest();
$request->setUrl('$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array('key'=>'START', 'value'=>'https://instagram.com/instagram')
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
If you want to try with cUrl the following code snippet will work.
$curl = curl_init();
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"content-type: multipart/form-data;"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

Categories