The following code runs perfectly using POSTMAN. While I try to use the code generated by postman for PHP, it gives me errors.
Original Code:
curl -X POST \
'http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token' \
-H 'cache-control: no-cache' \
-H 'gspappid: 64AB7894D61644F3B5CA18B4738C700D' \
-H 'gspappsecret: 70988876GEEEEG4504GB400G92B801E23686' \
PHP Code:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"gspappid: 64AB7894D61644F3B5CA18B4738C700D",
"gspappsecret: 70988876GEEEEG4504GB400G92B801E23686",
"postman-token: ff8278bb-b888-9fd3-6a55-780dafd58aa5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Please help me.
What kind of error are you facing , can you please share so we can help you?
If there is timeout error , you can try changing value of CURLOPT_TIMEOUT => 30
Related
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.
I have this code,
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://example.com/v1/items/id/preview",
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 => [
"Accept: application/json",
"Authorization: Bearer bearer-string",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
file_put_contents('file.jpeg', print_r($response, true));
}
From that code I can tell that the response is jpeg that's why I assigned file.jpeg as filename.
Now my problem, what if the response is not jpeg?
I could not find any function from https://www.php.net/manual/en/ref.filesystem.php that will dynamically determine what type of file is in response.
Any Idea about this.
You can use curl_getinfo to get content-type of what curl got
$mime = curl_getinfo(response , CURLINFO_CONTENT_TYPE);
and then simply check if $mime equals to 'image/jpeg'
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.
I try to get html result from a site using cURL in PHP, and I got 301 moved permanently, But when I use command line, it working and returning html string reuslt.
This is my Code PHP (result not success)
curl_setopt_array($curl, array(
CURLOPT_URL => "https://detail.1688.com/offer/535394058500.html?spm=b26110380.sw1688.mof001.302.6YIDM0&v=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "a=b",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: b60107e9-4350-e93f-d6cf-d626505687b8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response; // return 301 moved permanent
}
And this is in CMD (result success)
curl -X GET 'https://detail.1688.com/offer/535394058500.html?spm=b26110380.sw1688.mof001.302.6YIDM0' -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' -H 'postman-token: 2b11da6f-dcde-d527-289c-bfbf8a85d30c'
Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
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(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
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(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}