Accessing exchange rates converted into CAD from USD isn't working - php

I'm not sure why the test program below isn't getting the response from the API. Here is the document for the API. It's working absolutely fine on the provider's playground. Running the test program below doesn't do anything at all.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.apilayer.com/exchangerates_data/convert?to=CAD&from=USD&amount=1",
CURLOPT_HTTPHEADER => array(
"Content-Type: text/plain",
"apikey: av5CJRrOA2Rclj4fai3UZF5NT7F9HmPr"
),
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"
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>

Related

Cancel Shipstation API Issue PHP

I'm trying to cancel the order from Shipstation for That I've write a code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://ssapi.shipstation.com/orders/".$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 => "DELETE",
CURLOPT_USERPWD => "$public_key:$private_key",
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
echo "here";
now when I run this API it returns nothing no error no issue no success message same order is also not deleted from Shipstation when I try to pass wrong Public or Private key then it gives me an Error
401 Unauthorized
it means that API call is going correctly because I'm getting response but when I pass the correct creds then it gives no response.
Edit
Now I'm trying
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://ssapi.shipstation.com/orders/".$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 => "DELETE",
CURLOPT_HTTPHEADER => array(
"Host: ssapi.shipstation.com",
"Authorization:Basic __MY_AUTH_HERE__",
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Now it is giving me an Error
{"Message":"Authorization has been denied for this request."}
where I've created my Auth like
$auth = base64_encode($username_api_key.":". $password_api_secret);
What is the issue and how to fix that?
Thanks

PHP Curl GET request not working but works fine in POSTMAN

I am trying to get product data from google merchant center (google shopping ), my curl command works fine on postman, but not on php. Please help me
On postman:
On Php:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://shoppingcontent.googleapis.com/content/v2.1/436306321/products',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ya29.a0ARrdaM8TK4rZeVRnsj9MgcFg3STE1EqGvwj20J-REq8f6poCzGIv2MzUIegdSP1DDa_-HQpHIQNBTU6Rnxf7HsnWCjwkP7_pwkp-kr5zm0o0Z6EuZCVa4IdwHzX7MGM8SQ-94Vkxps4JRSbkgplYo4w1K8lPwQ'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

PHP - GET request works in Postman, but not with cUrl

There's a free weather API in my country that I want to use to get rain data. The request is simple https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826 and there's no authentication. If I try the request using Postman, my browser or AJAX, it works fine and I get the results I need. However, using cURL, I get nothing.
Here's the PHP code that I tried:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826',
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',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I have the cURL.dll installed and tested in another server, but it didn't work.
Is there a problem with the code or is it the API that blocks PHP requests?
its look like this website looking for user-agent header.
try this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826',
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(
'User-Agent: something'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

403 The request could not be satisfied response when hitting the Cowin API

403 ERROR The request could not be satisfied cowin API
getting above error while hitting the cowin API
This is working fine on local machine and post man
here is my code
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=512&date=16-05-2021%0A',
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',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>

Create Azure AD user by tenant and app ID

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

Categories