CORS Issue when Interacting with Instagram HTML in Laravel Guzzle

61 Views Asked by At

I'm working on a project where I need to log into Instagram programmatically within my Laravel application. I've successfully fetched the Instagram HTML on the server side and displayed it on the front end. However, I'm encountering CORS issues when interacting with Instagram, specifically when making login requests to Instagram.

Here's the error message:

Access to XMLHttpRequest at 'https://www.instagram.com/api/v1/web/accounts/login/ajax/' from origin 'http://127.0.0.1:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Here's the function

public function InstagramLogin(Request $request)
{
    $client = new Client();
    $cookieJar = new CookieJar();
    $ip = '15.204.130.149';
    $port = '8181';
    $username = '';
    $password = '';

    $proxyUrl = "$ip:$port";
    if ($username && $password) {
        $proxyUrl = sprintf(
            'http://%s:%s@%s',
            urlencode($username),
            urlencode($password),
            $proxyUrl
        );
    }

    $response = $client->request('GET', 'https://www.instagram.com', [
        'cookies' => $cookieJar,
        'proxy' => $proxyUrl,
    ]);

    $html = $response->getBody()->getContents();

    return $html;
}

Are there alternative approaches I should consider, or are there additional configurations that could help resolve this CORS issue? Any guidance or insights would be greatly appreciated. Thank you!

Note: I'm aware that Instagram provides an official API, but I'm exploring logging in without using the official API due to specific project constraints or requirements.

0

There are 0 best solutions below