I am debugging why am I getting 400 response every time from the Laravel server.
When I am calling using Postman, everything works fine, but when I call from my curl script it returns 400 everytime.
My curl code looks like this:
$endpoint = "http://sup.l/api/iasku/IA00000001-My Beat-29999-H?";
$additional_headers = "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zdXAubG9jYWxcL2FwaVwvbG9naW4iLCJpYXQiOjE2MDE2MjI2MDQsImV4cCI6MTYwMTcwOTAwNCwibmJmIjoxNjAxNjIyNjA0LCJqdGkiOiJITFAyUEhWeEdQU1J0NWFQIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.3t15l573A_EHotUq6Ud3fcGegXZh1tGsMf3i9BlrVWU";
$headers = array_merge([
'Content-Type: application/json',
'Accept: application/json',
], $additional_headers );
$options = [
CURLOPT_URL => $endpoint,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_RETURNTRANSFER => 1, // return web page
CURLOPT_HEADER => 0, // don't return headers
CURLOPT_FOLLOWLOCATION => 1, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "myuser.agent", // who am i
CURLOPT_AUTOREFERER => 1, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => 0, // Disabled SSL Cert checks,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HTTPHEADER => $headers,
];
if (count($body)) {
$options['CURLOPT_POSTFIELDS'] = json_encode($body);
}
$ch = curl_init();
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
My curl command looks like this (works fine):
curl --request DELETE \
--url http://sup.l/api/iasku/IA00000001-My%20Beat-29999-H? \
--header 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zdXAubG9jYWxcL2FwaVwvbG9naW4iLCJpYXQiOjE2MDE2MjI2MDQsImV4cCI6MTYwMTcwOTAwNCwibmJmIjoxNjAxNjIyNjA0LCJqdGkiOiJITFAyUEhWeEdQU1J0NWFQIiwic3ViIjoxLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.3t15l573A_EHotUq6Ud3fcGegXZh1tGsMf3i9BlrVWU'
Can anyone see what is the problem here? And how should I debug this?
The problem here was that my URL had spaces, so the server couldn't process it.
Once I added rawurlencode for this part of URL IA00000001-My Beat-29999-H? it started working as expected.
Related
I need to PUT some json data to an API endpoint, which works as expected via command line curl, but not via php curl and I don't have any idea, why it doesn't.
my command is
curl -v --insecure --request PUT --url <https://blabla/blablabla> --user 'username:password' --header 'Content-Type: application/json' --data '<valid json data>'
but it doesn't work this way within php:
// get cURL resource
$curl = curl_init();
// set cURL options
$curloptions = array(
CURLOPT_PUT => true, // set method to PUT
CURLOPT_RETURNTRANSFER => true, // return the transfer as a string
CURLOPT_VERBOSE => true, // output verbose information
CURLOPT_SSL_VERIFYHOST => false, // ignore self signed certificates
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERNAME => $config['uag']['user'], // set username
CURLOPT_PASSWORD => $config['uag']['pass'], // set password
CURLOPT_HTTPHEADER => array( // set headers
"Content-Type: application/json",
),
CURLOPT_POSTFIELDS => $jsondata // set data to post / put
);
curl_setopt_array($curl, $curloptions);
foreach($serverurilist as $uri) {
// set url
curl_setopt($curl, CURLOPT_URL, $uri);
// send the request and save response to $response
$response = curl_exec($curl);
// stop if fails
if(!$response) {
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
var_dump($response);
}
// close curl resource to free up system resources
curl_close($curl);
What doesn't work? The payload / data doesn't get submitted. If I tcpdump the command line und php version without encryption, I can see, that the command line submits the data right after the Expect: 100-continue request and the HTTP/1.1 100 Continue response from the server. The php version doesn't do anything after the HTTP/1.1 100 Continue response and quits after reaching the timeout.
From documentation:
CURLOPT_PUT - true to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
and you are not using any file to provide content.
You should use CURLOPT_CUSTOMREQUEST => 'PUT'.
This is your same cUrl request exported from Postman:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://blabla/blablabla',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'<valid json data>',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ='
],
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
You should use this CURLOPT_CUSTOMREQUEST=>'PUT'
everybody,
I am using the following code for synchronizing data with an API ($method= POST) and then I call using the GET method ($method= GET) to print the result of the synchronization:
function sendCurl ($fileName, $method)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($fileName)),
CURLOPT_HTTPHEADER => array(
"Authorization: xxxxxxxxxxxxxxx",
"Accept: application/json."
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
The problem is that the GET method response arrives before the API has time to process it. Therefore, when I print the response I print WAITING FOR RESPONSE and not whether it went ok or failed.
What can I do to print the ok or fail and not the WAITING FOR RESPONSE?
Thank you very much!
I am using PHP curl to do basic Auth that return a jwt token.
When I Request the token in shell using curl i get a token of length 381 .However when I do the request in my php code i only get a token of length 326 which is not complete which lead to other problems in my other php requests .
how can i make my php code give me the full length of my token that is 381 in my response and not just 326?
Update my cURL shell code is :
curl --location --request POST 'https://xxxx/xxxx/auth/login' \
--header 'Authorization: Basic XXXXX'
my curl php code :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxxx/xxxxx/auth/login",
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_HTTPHEADER => array( "Authorization: Basic xxxxxxx" ),
) );
$response = curl_exec($curl);
curl_close($curl); echo $response;
Thanks
Here is my code:
// Build request URL
$url = 'https://connect.squareup.com/v2/locations/';
// Build and execute CURL request
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
var_dump($content);
Here is what I get back:
string(158) "{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"NOT_FOUND","detail":"API endpoint for URL path `/v2/locations/` and HTTP method `GET` is not found."}]}"
I am pounding my head on this one... I tried using the Square SDK but calling from it doesn't return a list of locations either.
I have created an application on the Square Developer Dashboard. $accessToken is set to the sandbox access token listed there.
You have added an extra / at the end of the url. You should instead use:
$url = 'https://connect.squareup.com/v2/locations';
Other than that your code works!
I'm trying to curl to PayPal to create payment URL, and I've bought a cert meet PayPal requirement.
When I first call the curl it return error
NSS: client certificate not found (nickname not specified)
But when I try to reload the page within few second the curl return success.
Is there any way for me to set the cert in curl?
I've read the other ansewer but some links were dead and the answer does not work for me.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/payments/payment",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"intent":"sale","redirect_urls":{"return_url":"'.$return.'","cancel_url":"'.$cancel.'"},"payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"'.$amount.'","currency":"HKD"}}]}',
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $token",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 7ca12352-aa45-2c4e-d87d-fdd09795121d"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
// If I added this refresh code it works!?
/*
if ($err) {
header("Refresh:0");
}
*/