0

I am trying to use the pokeapi.co api and it works fine when I make a file_get_contents request to it on my localhost, but when I put the same file onto my server, it does not work. Another api I have tested has worked and I have the allow_url_open set to on in the ini on my domain, but I still can't seem to get the file_get_contents to work.

<?php
    $json = json_decode(file_get_contents('http://pokeapi.co/api/v2/pokemon/charmander'));
    echo '<p>'.$json->id.'</p>';
?>
  • 1
    Define "does not work". What error do you get? – ceejayoz Oct 26 '17 at 01:58
  • Where is your code? – miknik Oct 26 '17 at 01:58
  • @miknik Oops that's a bad thing to forget – The42ndTurtle Oct 26 '17 at 02:11
  • @ceejayoz as of now, nothing. It doesn't return an error and it also doesn't return the thing it's supposed to – The42ndTurtle Oct 26 '17 at 02:12
  • 1
    Check the PHP error log. It will tell you what error you get. – Pia Wurtzbach Oct 26 '17 at 02:13
  • Add this line below your code. What is the output? var_dump($http_response_header); – miknik Oct 26 '17 at 02:41
  • I get a 301 response unless I change http to https and add a / to the end of the url. Then I get a 200 – miknik Oct 26 '17 at 02:43
  • What is the configuration value of `allow_url_fopen` on your server? – aknosis Oct 26 '17 at 02:47
  • @Aknosis it's set to 'On' – The42ndTurtle Oct 26 '17 at 14:05
  • @miknik if I am reading it correctly, I am getting 403 Forbidden. I am getting that with and without the https and the / at the end – The42ndTurtle Oct 26 '17 at 14:09
  • Is it running on a shared server? – miknik Oct 26 '17 at 17:07
  • @miknik I have no clue how to tell. I am using ipower (I know it sucks) so I don't know if it is shared hosting – The42ndTurtle Oct 26 '17 at 19:37
  • If its cheap and it sucks then it sounds like it's shared. My guess is someone else on your hosting plan who is sharing your IP address, or you by mistake have disrespected the fair use policy of the API and now your server IP address is banned. No pokeapi for you I'm afraid. – miknik Oct 27 '17 at 01:28
  • @miknik well, I contacted the 24/7 live support and the guy said he changed the url_fopen in the ini so it should work (it didn’t) and he didn’t even change anything (compared it to old version) and I said it still didn’t work and now I’m on hold to talk to their php specialist so wish me luck. I really hope that it’s not ip banned like you said. I haven’t used this api before, so I don’t think it was me that got it blocked – The42ndTurtle Oct 27 '17 at 01:47
  • Its not a config issue, you are getting a 403 response from the server so your request is reaching the API and being processed and returned, but they are denying your request. Going by the documentation on their site it looks like your IP is banned to me. – miknik Oct 27 '17 at 01:58

0 Answers0