简体   繁体   中英

Instagram API with CURL slow, jquery ajax fast

I have problem with Instagram api using PHP. Instagram API url is following:

https://api.instagram.com/v1/media/popular?client_id=c32bb75ba2b449dc93b5461a84d9003e

Using this: https://github.com/cosenary/Instagram-PHP-API

with following code in my php script:

<?php 
$instagram = new Instagram('c32bb75ba2b449dc93b5461a84d9003e');
$response = $instagram->getPopularMedia();
header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);

script execute sometimes longer then 40sec, sometimes no results.

Same problem is with simple call:

<?php  
echo file_get_contents('https://api.instagram.com/v1/media/popular?client_id=c32bb75ba2b449dc93b5461a84d9003e')

With simple jquery AJAX request like this(on same server):

$.ajax({
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/media/popular?client_id=c32bb75ba2b449dc93b5461a84d9003e",
        success: function(data) {
            console.log(data);
        }
    });

result come always very fast.

I try this two ways on different servers and results are same. Also in PHP class I try use file_get_content instead CURL but everything is same.

Please help (with PHP).

  • When using AJAX, you (speaking of your web browser) opens the connection to instagram.
  • When using PHP, your web server opens the connection to instagram.

This means: the connection from your web server to instagram is probably slow because either your web server is far away or uses a very bad route to it or (the more probable explanation) you (your code) are sitting on a very poor hoster which limits bandwith from scripts within web servers.

You can debug that by trying to get contents of other web services or web sites.

add Connection: close to the HTTP header you send. in PHP: header('Connection: close');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM