issue with PHP/Curl using Auth0 - php

I am attempting to use Auth0 to secure an API on Laravel Framework 8.83.22
I am using this tutorial (machine-to-machine) https://auth0.com/blog/build-and-secure-laravel-api/
Problem:
Curl at the command line works fine:
curl -X GET -H "Authorization: Bearer ***REMOVED***" -H "Content-Type: application/json" -d "{}" http://localhost:8000/api/weightrecords/4195
However, making the call inside a PHP script times out (I've tried longer timeouts with no help). I get the token fine (SECTION 1 below) but SECTION 2 times out (cURL Error #:Operation timed out after 30000 milliseconds with 0 bytes received).
public function test_api()
{
//SECTION 1: Get the token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://**REMOVED***.auth0.com/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"client_id\":\"qDpVdykOlRYkxOdq8J7a2CJ7X7E3WUK8\",\"client_secret\":\"**REMOVED**\",\"audience\":\"**REMOVED**",\"grant_type\":\"client_credentials\"}",
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response_array = json_decode($response);
$access_token = "authorization: Bearer " . $response_array->access_token;
echo $access_token;
}
//SECTION 2: Use the token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_VERBOSE => true,
CURLOPT_URL => "http://127.0.0.1:8000/api/weightrecords/4195",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
$access_token
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
.env
AUTH0_STRATEGY=api
AUTH0_DOMAIN=**REMOVED**.us.auth0.com
AUTH0_CLIENT_ID=qDpVdykOlRYkxOdq8J7a2CJ7X7E3WUK8
AUTH0_AUDIENCE=**REMOVED**
API_IDENTIFIER=**REMOVED**
kernal.php
protected $routeMiddleware = [
//...all the defaults...
'jwt' => \App\Http\Middleware\CheckJWT::class, //<<<<------Added
];
api.php
Route::apiResource('weightrecords', ApiController::class);
CheckJWT.php and auth.php same as in article..

What does $access_token look like?
Try using the post data without the escapes.
You may need to urlencode() the json. Typically not, but I have had it help with one API.
This is how I begin the trouble shooting process.
Add these options.
CURLOPT_CONNECTTIMEOUT=>10,
CURLOPT_FAILONERROR=>true,
CURLINFO_HEADER_OUT=>true,
Get the curl_getinfo:
$response = curl_exec($curl);
$err = curl_error($curl);
$info = var_export(curl_getinfo($curl),true);
echo $info;
You should see the out going request header in $info because of the CURLINFO_HEADER_OUT=>true.
I have a PHP script where it returns details about the request including Body, $_POST, $_GET, $_REQUEST, $_SERVER
LINK to see your request details
You can use this link as your curl URL.

Related

How to fix error "cURL Error #:transfer closed with outstanding read data remaining" on PHP

I try curl on php and i have curl syntax on Lumen 5.*, but if data is big can't running
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Cache-Control: no-cache',
'Content-Type: application/json',
'Authorization: Bearer blablabla'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo 'cURL Error #:' . $err;
} else {
if ($this->isJSON($response) == False) {
return $response;
}
return json_decode($response, true);
}
but if data is big show error
cURL Error #:transfer closed with outstanding read data remaining
how to fix it?
You have to set the KEEPALIVE and KEEPIDLE parameters in curl.
This is discussed also in the past, in guzzle community and that was the way to tackle it. Since Guzzle users curl under-the-hood this solution should match your needs as well.
CURLOPT_TCP_KEEPALIVE => 10,
CURLOPT_TCP_KEEPIDLE => 10
Source

How do I find out how to translate a Postman query in cURL via PHP

I use postman for the first time.
My goal is to download a JSON file.
I managed to look at the code for cURL in postman:
But if I test the code in the command line, I can not log in.
Actually, I want to call the JSON file via PHP. I have tested initial approaches. In addition to the problem that I can not authenticate myself here, I think that I need more values. For example POSTFIELDS. Is that correct and if so, how do I find what I have to enter in Postman? Here is my initial Code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.divessi.com/divessi/index.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "..",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic BASE64",
"Cache-Control: no-cache",
"Postman-Token: TOKEN",
"content-type: ??"
),
));
$response = curl_exec($curl);
$obj = json_decode($response);
$events = array();
if ($obj)
{
$events = $obj[0]->DATA;
}
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
} else {
foreach ($events as $event) {
...
}

API returns NULL response sometimes

I am making a API request in my web app which looks like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://******.com/jdconnectionpool/view?
requestAction=JOB_FILE_DETAILS&job_no=7476709",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Everything looks to be fine. However, it returns null sometimes for the same set of params on calling multiple times. I do not seem to find any pattern either. Any far fetched information from someone who has experienced this phenomenon would be really appreciated.

PHP CURL give me 401 error but work fine on POSTMAN

I am calling two API one login and second one for add user. To add user first i need to call login API then add user API. when i test these API in postman it work fine but when i call using CURL for add user i am getting 401 error.
Login API in PostMan
Login API
Add User API in POSTMAN
User API
login API PHP code which work fine
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/sapi/login?action=login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "login=login&password=password",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
but when i call add user api with the following code it give me error.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/sapi/profile/generic?action=add",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>
'{"data":{"user":{"generic":{"userid":"Hagrid","password":"abc123","firstname":"afds","lastname":"asf","useremail":"test#gmail.com"}}}}',
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
add the below code before curl_exec
curl_setopt($curl, CURLOPT_COOKIEJAR, "COOKIE.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, "COOKIE.txt");

PHP cURL with API Key and Secret (Postman)

I'm working with an API which is made by a classmate. I used Postman to generate the PHP cURL for the connection, with Authorization Basic. This works perfectly. Now, I want to get rid of the Authorization Basic and use my own API Key & Secret (password). Are there any good ways to do this?
Thanks in advance!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "Some link here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic //something here",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response,true);
}

Categories