What authentication system are you using? Sanctum? You could create an access token for your "WordPress" user and pass that in the header Authorization: Bearer <TOKEN> and you should be good to go.
Jul 9, 2021
2
Level 1
Laravel 8 - Access private laravel route from another application
I am using Laravel Framework 8.47.0.
I am trying to access a private API from my laravel application via a wordpress script.
I have created the following code to log in via curl and access the API:
$BASE_URL = "https://mywebsite.com/";
$module = "";
try {
// get json file from api
$link = $BASE_URL . "hiddenApi";
$username = '[email protected]';
$password = 'systemPassword';
$channel = curl_init();
// Debugging
// curl_setopt($channel, CURLOPT_VERBOSE, true);
curl_setopt($channel, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($channel, CURLOPT_URL, $link);
curl_setopt($channel, CURLOPT_USERPWD, "$username:$password");
curl_setopt($channel, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($channel, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($channel, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($channel, CURLOPT_TIMEOUT, 0); // never times out during transfer
curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($channel, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($channel);
$info = curl_getinfo($channel);
...
When executing the above code from my wordpress application I get as output the login-page.
Is there a better way to implement this?
Any suggestions what I am doing wrong?
I appreciate your replies!
Please or to participate in this conversation.