laravel 7 keeps adding '/public/' to my api routes - php

I am trying to make a php curl request to my laravel api
Here's the curl function
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HEADER, array('Accept:application/json', 'X-Requested-With:XMLHttpRequest' )
);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
print curl_error($curl);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Here's where I call it :
require_once ADJEMINPAY_PLUGIN_DIR.'includes/class-adjeminpay-functions.php';
$url = "https://api.adjeminpay.net/v1/auth/getToken/";
$postData = array(
'firstName' => 'Lady',
'lastName' => 'Gaga',
'submit' => 'ok'
);
$result = CallAPI("POST", $url, $postData);
var_dump($result);
Here's the result :
string(454) "HTTP/2 301 date: Fri, 04 Dec 2020 12:12:03 GMT server: Apache location: https://api.adjeminpay.net/public/v1/auth/getToken content-length: 258 content-type: text/html; charset=iso-8859-1
Moved Permanently
The document has moved here.
"
Looks like laravel doesn't get that it's an api request and keeps changing the url to https://api.adjeminpay.net/ public/ v1/auth/getToken/ ; it also tries to redirect to https://api.adjeminpay.net/
Help kudasai TT

Sooo, turns out the syntax/version of curl I was using was defective/uncomplete.
Went over to PostMan and executed the same request then copipasted the php -curl code which looks like this :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'yourUrl',
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 =>'{
"apikey": "qsmdlfjqs",
"application_id": "qsdlfqsmlj",
"grant-type": "qmdsfqsdf"
}',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
': ',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it works fine now.
Arigathanks \(^^)/
By the way How to generate PHP -curl code from POSTMAN.

Related

Convert Ajax POST script to PHP cURL POST Script

I'm trying to make a post call like this to return a jwt bearer token. But in php using cURL. So I can save the bearer token to a variable for use in the API calls.
$.ajax({
type: 'POST',
url: 'http://www.your-site.com/siteguard/api/prepare-public-jwt',
data: { api_key: 'YOUR_PUBLIC_API_KEY' },
dataType: 'json',
success: function (data) {
//Returns encoded JWT
console.log(data);
}
});
Php Code Im using
$url = $this->getTokenUrl();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = ["api_key" => "xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX"];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
The Return is this
{"code":401,"status":"error","response":"Invalid Public API key","link":"http:\/\/localhost\/siteguard\/api\/prepare-public-jwt"}"
I was able to achieve it use this
$url = $this->getTokenUrl();
$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 => 'POST',
CURLOPT_POSTFIELDS => 'xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$obj = json_decode($response);
curl_close($curl);
return $obj->response;

Fetch JSON response from API using php

I'm trying to fetch data from API, but no data returns.
I try the API request in postman and it's working but in my php code nothing gets returned.
$uri = 'https://example.com';
$opts = array(
'http' => array(
'method'=>'POST',
'header'=>'Content-Type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$result = file_get_contents($uri, false, $context);
print $result;
or using curl
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com',
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',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
the error is
file_get_contents(https://example.com): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required
Note: the response size in postman is 9.83, is this maybe an issue?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
The issue has been solved by add Content-Length: 0 in the header

PHP request to API endpoint with authorized header

I would like to ask you how to use PHP request to API with authorized header.
API end point example is
Quotation Service : ­ POST https://api.website.com/v4/quotation
Content­type: application/json; charset=utf­8
Accept: application/json
Authorization: hmac $id:$milliSeconds:$signature
{
"serviceType": "CARD",
"specialRequests": [
"ROUNDTRIP"
]}
as you mention, they use hmac as an authorization in a header and request parameters is serviceType, specialRequest
I try using PHPCurl to make a request but i'm not a good on it. Please give me and advice how to use Curl with this API.
edit:
<?php
$customerId = "585a-SAMPLE-980dfe0097"; //by provider
$privKey = "MC4CAQACBQDSk4ghAgMB---SAMPLE---QIDAOPFAgMAk7QIDAMYq"; //by provider
$requestTime = (int)(microtime(true) * 1000);
$signature = hash_hmac('sha256', $requestTime, $privKey);
$headers = array(
"Authorization: hmac $customerId:$requestTime:$signature"
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.samplesite.com/v1/quotations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Regards,
Hmm ... maybe something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/v4/quotation');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
/* if you want to use SSL certificate, otherwise delete this */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAPATH , '/path/to/certificate');
$auth = hash_hmac('sha1', $id.':'.$milisecond.':'.$signature, <ENCRYPTION KEY>, true);
$headers = array(
'Authorization: '.$auth
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

curl request with headers in PHP

I want to make a cURL request to a URL along with the following headers :
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXXX'
I have the following code :
<?php
$post_url = "https://api.interlinkexpress.com/user/?action=login";
$curl = curl_init($post_url);
$headers = array(
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXX'
);
//curl_setopt($curl, CURLOPT_USERPWD, "username":"Password");
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post_data) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$post_response = curl_exec($curl);
?>
it is showing the result as :
Client sent a Bad Request
which is the 'Authorization...' line under the $header array
Any suggestions/help is appreciated.
problem is in header.It needs to be fixed.authorization will e whole string like this:
$headers = array(
'Content-Type: application/json',
'Authorization: Basic XXXXXXXXX'
);
let me give you a full example of curl:
function CurlSendPostRequest($url,$request)
{
$authentication = base64_encode("username:password");
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $authentication",
"Content-Type: application/json"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
return $data;
}
500 Error typically suggests that the error is on the server side. You can probably try to emulate your request ( I would personally recommend using Advanced ReST Client, which is a google-chrome app), to verify whether this is the case.

CURL set CURLOPT_POST perminetelly

I'm getting a very strange behavior when I do multiple requests with curl. Here's the function I have:
function http_request($curl, $url, $post = null)
{
echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;
try
{
$cookie_path = tempnam(null, 'b');
curl_setopt_array($curl, array
(
CURLOPT_COOKIEFILE => $cookie_path,
CURLOPT_COOKIEJAR => $cookie_path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => count($post),
CURLOPT_POSTFIELDS => $post,
CURLOPT_HEADER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
CURLOPT_HTTPHEADER => array
(
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
)
));
return curl_exec($curl);
}
catch(Exception $exception)
{
echo $exception;
}
}
Here's the code I use to call it:
$curl = curl_init();
http_request($curl, "http://host.com/url1");
http_request($curl, "http://host.com/url2", "postdata=123");
http_request($curl, "http://host.com/url3");
http_request($curl, "http://host.com/url4");
curl_close($curl);
This is the output I'm getting:
GET http://host.com/url1
POST http://host.com/url2
GET http://host.com/url3
GET http://host.com/url4
So far so good, but using packet analyzer (wireshark), the output looks like this:
POST http://host.com/url1
Content-Length: 0;
POST http://host.com/url2
Content-Length: 12;
POST http://host.com/url3
Content-Length: 0;
POST http://host.com/url4
Content-Length: 0;
Then I rewrote the code like this:
function http_request($curl, $url, $post = null)
{
echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;
try
{
$cookie_path = tempnam(null, 'b');
curl_setopt_array($curl, array
(
CURLOPT_COOKIEFILE => $cookie_path,
CURLOPT_COOKIEJAR => $cookie_path,
CURLOPT_COOKIESESSION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
CURLOPT_HTTPHEADER => array
(
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
)
));
if($post != null)
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}
return curl_exec($curl);
}
catch(Exception $exception)
{
echo $exception;
}
}
but still the same thing happens, if I remove this code from the function:
if($post != null)
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}
In packet analyzer I get:
GET http://host.com/url1
GET http://host.com/url2
GET http://host.com/url3
GET http://host.com/url4
It makes no sense how it gets set to POST on the first request, even my $post argument is not set. Thanks!
I vaguely remember I had the same problem some time ago. Try setting CURLOPT_HTTPGET explicitly as well:
if($post != null)
{
curl_setopt($curl, CURLOPT_HTTPGET, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
else
{
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
}

Categories