Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GregorSams's avatar

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!

0 likes
2 replies
CorvS's avatar

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.

automica's avatar

Where are you placing the route that you are trying to curl to?

If it’s in web.php and under auth you’ll want to move it into api routes and use a bearer token in your headers to authenticate.

Depending on how complex your api is, you’ll likely be able to use sanctum for your authentication

https://laravel.com/docs/8.x/sanctum

Please or to participate in this conversation.