I'm trying to get the access token - php

$curl = curl_init();
$auth_data = array(
'client_id' => 'xxxxxxxx', // i have it
'client_secret' => 'xxxxxxxx', // I have it
'grant_type' => 'client_credentials'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, 'https://xxxxxx.caspio.com/oauth/token'); // I have it
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
I would like to know why I'm getting Bad Request. When I try to show the errors it shows nothing.

This worked for me
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => "<TOKEN ENDPOINT URL>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"grant_type=client_credentials&client_id=<client_ID>&client_secret=<Secret>",
));
$response = curl_exec($curl);

Related

Change curl on stream_context_create

Pls, help me to get data with stream_context_create.
With curl work that:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, 'ecdhe_rsa_aes_128_gcm_sha_256');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$out = curl_exec($curl);
$curlError = curl_error($curl);
$result = json_decode($out, true);
curl_close($curl);
I try this:
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peername' => false
),
'http' => array(
'method' => 'GET',
'header' =>
'Content-Type: application/json'
)
);
$context = stream_context_create($opts);
$result = file_get_contents('$url', false, $context);
var_dump($result);
But get false. What in my code wrong?
I got the decision:
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'http' => array(
'timeout' => 5,
'method' => 'GET',
'header' =>
'Content-type: Content-type: application/json',
'ignore_errors' => true,
'max_redirects' => 0
)
);
$context = stream_context_create($opts);
$out = file_get_contents($url, false, $context);
$result = json_decode($out, true);

convert curl post to wp_remote_post

Here is curl code which is working very good
$data = array(
"to" => $to ,
"from" => $options['from_email'] ,
"subject" => $subject,
"body" => $message,
);
$url = 'https://example.com';
$api_key = 'apikeyhere'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
));
$response = curl_exec($ch);
But if i try to convert this above code to worpress wp_remote_post i am getting error.
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
),
'body' => json_encode($data),
) );
here is response i am getting
https://pastebin.com/Ap5LpfZb
Please let me know where i am doing wrong ?
I got it there was problem with the header array i was passing that array wrongly
Wrong code was
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
),
'body' => json_encode($data),
) );
Here is correct version of code
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept'=> 'application/json',
'Content-Type' =>'application/json',
'X-API-KEY' => $api_key,
),
'body' => json_encode($data),
) );

Curl Response takes very long

my Curl Response takes very long. Is there any way to make it faster?
This is an api for checking the status from elevators.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.deutschebahn.com/fasta/v2/facilities?type=ELEVATOR&stationnumber=1401",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
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 XXXXXXXXXXX",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($response, true);

Is there any function to send airtime using Reloadly API

I have tried reloadly API stated on their doc but no success, i could not find exactly the correct API end point to make Curl call.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://topups.reloadly.com/accounts/balance
");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/com.reloadly.topups-v1+json",
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik0wWXpRa"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I got from their their API doc https://topupsapi.docs.apiary.io it stated on sending airtime but no correct endpoint stated. thank
Is there any function or correct endpoint i didn't know about?
the endpoint is https://topups.reloadly.com/topups , and it's supposed to look something like this:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://topups.reloadly.com/topups',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array(
"Accept: application/com.reloadly.topups-v1+json",
"Content-Type: application/json",
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSU",
),
CURLOPT_POSTFIELDS => json_encode(array(
'recipientPhone' => array(
'countryCode' => 'HT',
'number' => '+50936377111',
),
'senderPhone' => array(
'countryCode' => 'US',
'number' => '+13059547862',
),
'operatorId' => 173,
'amount' => 15,
'customIdentifier' => 'transaction by john#example.com',
))
));
curl_exec($ch);
curl_close($ch);
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://topups.reloadly.com/topups',
CURLOPT_POST => 1,
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/com.reloadly.topups-v1+json",
"Authorization: Bearer your_access_token")),
CURLOPT_POSTFIELDS => json_encode(array(
'recipientPhone' => array(
'countryCode' => 'HT',
'number' => '+50936377111'
),
'senderPhone' => array(
'countryCode' => 'US',
'number' => '+13059547862'
),
'operatorId' => 173,
'amount' => 15,
'customIdentifier' => 'transaction by john#example.com'
))
));
$response = curl_exec($ch);
curl_close($ch);
echo '<pre>';
var_dump($response);
echo '</pre>';
[enter image description here][1]?>

New RightSignature API "public/v1/sending_requests" throws invalid_scope error

The following is my script. I am continuously getting an invalid_scope error.
$arr = array(
'file' =>
array(
'name' => 'pdf.pdf',
'source' => 'upload',
),
'document' =>array(
'signer_sequencing' => 'false',
'expires_in' => '12',
'name' => 'Signme',
'roles' =>array(
'name' => 'a',
'signer_email' => 'example#gmail.com',
'signer_name' => 'farqalit',
),
),
'sending_request' =>array(),
);
$post_json = json_encode($arr);
$endpoint = "https://api.rightsignature.com/public/v1/sending_requests?access_token=API_TOKEN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

Categories