0

I need some advice. I need to get data from API to display it on my application. The API is working both through Postman or through the browser, but when I upload the same code to the server, my API response returns NULL.

Here is the response I am getting with POSTMAN:

Screenshot1

And here is the code I am using:

$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://www.instagram.com/myexcellentuser/?__a=1');
  curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  $file_data = json_decode(curl_exec($ch));
  curl_close($ch);
  var_dump($file_data);

And here is what this same code returns on the app hosted on a server:

Screenshot2

I would be extremely grateful for the advice!

Thanks!

JNevill
  • 46,980
  • 4
  • 38
  • 63
Newby
  • 1
  • 1
  • 1
    Are there any errors, cURL errors or PHP errors? What have you tried to do to debug this so far? – Martin Nov 13 '19 at 13:57
  • 1
    `echo curl_exec($ch);` to see what you get, maybe it's not JSON. – AbraCadaver Nov 13 '19 at 13:58
  • I used curl_error to see if there are any errors, but it returned nothing. – Newby Nov 13 '19 at 13:59
  • comment out this line `// $file_data = json_decode(curl_exec($ch));` and replace with: `$file_data = curl_exec($ch);` [source](https://stackoverflow.com/questions/25695993/php-curl-always-returns-null) – Martin Nov 13 '19 at 13:59
  • @Newby then if cURL is ok you need to reivew the next step - the JSON bit. – Martin Nov 13 '19 at 14:00
  • 1
    @Martin, tried your way, commented out my initla $file_data and replaced with curl_exec. Tried to var_dump new $file_data, but got: ("curl_exec(): supplied resource is not a valid cURL handle resource"). – Newby Nov 13 '19 at 14:07
  • Please write this line and see status code, maybe it has error: $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); – Ramin Rezazadeh Nov 13 '19 at 14:11
  • @RaminRezazadeh returns 0 – Newby Nov 13 '19 at 14:13
  • This error says the URL not reachable, please check the URL is accessible or not. I can not access to this URL. – Ramin Rezazadeh Nov 13 '19 at 14:16
  • This code works on local server. – Newby Nov 13 '19 at 14:17
  • @RaminRezazadeh for security reasons, I changed my users account name to myexcellentuser – Newby Nov 13 '19 at 14:18
  • @Newby When you did the "tried your way, commented out my initla ... " test, did you put the new `curl_exec()` call _after_ your `curl_close()` bychance? – Patrick Q Nov 13 '19 at 14:21
  • @PatrickQ ```$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.instagram.com/holywoodeyewear/?__a=1'); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_close($ch); $file_data = curl_exec($ch); print_r($file_data);``` – Newby Nov 13 '19 at 14:24
  • 1
    This is exactly what I was talking about: `curl_close($ch); $file_data = curl_exec($ch);` You are closing the curl handle before executing it. So, obviously, that's not going to work. – Patrick Q Nov 13 '19 at 14:26
  • 1
    It look like the SSL is missing for CURL Options. Check the link https://stackoverflow.com/a/44372276/310160 – balakrishnan Nov 13 '19 at 14:29
  • @PatrickQ, You were right, but, the result is still the same - NULL – Newby Nov 13 '19 at 14:30
  • @balakrishnan, tried to add ```curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);```. But the result is the same. This is some kind of mistery – Newby Nov 13 '19 at 14:33
  • 1
    Instagram blocks requests to non-API endpoints like this from most major server providers, as scraping is against their terms of service. – ceejayoz Nov 13 '19 at 14:36
  • @ceejayoz, But why it allows the same requests from my local server? So in other words, there is no way around this? – Newby Nov 13 '19 at 14:39
  • 1
    @Newby It allows the same requests from your local because you're not in a blocked big server provider's IP range when you're at home. There's likely no way around this - Instagram takes a lot of steps to prevent scraping. (You'll probably find your local hits a captcha after a while, too.) – ceejayoz Nov 13 '19 at 14:40
  • @ceejayoz is right. Your server is likely blocked by Instagram since that API is not available for public consumption. I tried your code using my username and it worked in my local and server. – Jim.B Nov 13 '19 at 15:13
  • @Newby the way around it is to either use the new API, or to route your requests through proxies. Do you have another server to try on? – David Nov 13 '19 at 15:35
  • @David, I will try to use a proxy. Thank you! Currently, I just saved the JSON as a text file and fetching it on the server. – Newby Nov 13 '19 at 17:21

0 Answers0