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

obink's avatar
Level 1

how to do an Instagram CURL in Laravel 8?

I'm new at Laravel and trying to do an Instagram CURL in Laravel.

previously(before Laravel), I put a CURL in a root/PHP/curl.php

function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
// Pull and parse data.
$result = fetchData("https://graph.instagram.com/me/media?fields=id,media_url,permalink&access_token=this1st0k3n");
$result = json_decode($result, true);

and using <?php include "php/curl.php";?> at the most top of the page.

but in Laravel otherwise, I tried using a different method,

I tried to put the fetch data in a controller,

or I put it in resource/views/php/curl.blade.php so I called it on the top of the page using @include,

or I just plainly script on the same page like this

{{

function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
// Pull and parse data.
$result = fetchData("https://graph.instagram.com/me/media?fields=id,media_url,permalink&access_token=this1st0k3n");
$result = json_decode($result, true);
$limit = 6;

}}

@foreach ($result['data'] as $post)
@for ($i = 0; $i < $limit; $i++)
<div class="col-sm-6 col-md-6 col-lg-4 events">
    <div class="portfolio-item">
       <div class="gallery-thumb">
           <a href=" {{$post['permalink']}}"target="_blank">
             <img src="{{$post['media_url']}}" class="img-responsive" alt=""></a>
       </div>
    </div>
 </div>
@endfor
@endforeach

none of these are working, please help me.

0 likes
3 replies
neilstee's avatar

@obink instead of using {{ }} use @php @endphp directive instead:

@php
function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
// Pull and parse data.
$result = fetchData("https://graph.instagram.com/me/media?fields=id,media_url,permalink&access_token=this1st0k3n");
$result = json_decode($result, true);
$limit = 6;

@endphp

// then the rest of code goes here...

obink's avatar
Level 1

hi, @neilstee it shows an error of Invalid argument supplied for foreach()

I was hoping someone teach me about guzzle in here tho. but if the primitive scripting PHP like this working, I'll take my chances.

neilstee's avatar

@obink

@php 
$response = Http::withOptions([
    'debug' => true,
])->get('https://graph.instagram.com/me/media?fields=id,media_url,permalink&access_token=this1st0k3n');

// from here you can decide how you want to print it based on the data you get
dd($response); 
@endphp 

Please or to participate in this conversation.