Search code examples
phpwordpressapiunirest

Unirest doing different things on local and live when accessing API


I have set up an API call to IGDB.com and on local everything is working perfectly there is not issues at all. When on live though it is doing loads of different things, not returning any body in the array and throws it in the ['headers'] or just asking for the key when I have already set it.

$headers = array(
    'Accept' => 'application/json',
    "X-Mashape-Key" => "KEY IS NORMALLY HERE"
);
$query = array(
    'fields' => 'name',
    'limit' => 10,
);

//$body = Unirest\Request\Body::form($query);

$response = Unirest\Request::get('https://igdbcom-internet-game-database-v1.p.mashape.com/games/?', $headers, $query);

Using the exact same code does this when I var_dump the response.

On Local

object(Unirest\Response)#975 (4) { ["code"]=> int(200) ["raw_body"]=> string(121) "[{"id":2411},{"id":2410},{"id":2409},{"id":2408},{"id":1213},{"id":1211},{"id":1210},{"id":1208},{"id":1207},{"id":1206}]" ["body"]=> array(10) { [0]=> object(stdClass)#979 (1) { ["id"]=> int(2411) } [1]=> object(stdClass)#1003 (1) { ["id"]=> int(2410) } [2]=> object(stdClass)#981 (1) { ["id"]=> int(2409) } [3]=> object(stdClass)#976 (1) { ["id"]=> int(2408) } [4]=> object(stdClass)#977 (1) { ["id"]=> int(1213) } [5]=> object(stdClass)#978 (1) { ["id"]=> int(1211) } [6]=> object(stdClass)#996 (1) { ["id"]=> int(1210) } [7]=> object(stdClass)#997 (1) { ["id"]=> int(1208) } [8]=> object(stdClass)#999 (1) { ["id"]=> int(1207) } [9]=> object(stdClass)#1000 (1) { ["id"]=> int(1206) } } ["headers"]=> array(8) { [0]=> string(15) "HTTP/1.1 200 OK" ["Content-Type"]=> string(9) "text/json" ["Date"]=> string(29) "Mon, 13 Feb 2017 22:50:58 GMT" ["Server"]=> string(13) "Mashape/5.0.6" ["X-RateLimit-requests-Limit"]=> string(4) "7000" ["X-RateLimit-requests-Remaining"]=> string(4) "6755" ["Content-Length"]=> string(3) "121" ["Connection"]=> string(10) "keep-alive" } }

On Live

object(Unirest\Response)#1 (4) { ["code"]=> int(401) ["raw_body"]=> bool(false) ["body"]=> NULL ["headers"]=> array(1) { ["{"message""]=> string(123) ""Missing Mashape application key. Go to http:\/\/docs.mashape.com\/api-keys to learn how to get your API application key."}" } }

I can't seem to work out why it works great on local but on live it is doing something completely different.

I am quite new to Unirest so debugging it is quite hard for me.

Thanks!

UPDATE: This has really got me stumped. It seems on live the returned body is being added to the headers array. Which should not be happening. Any ideas?


Solution

  • So after a lot of debugging, it was actually a PHP opendir security problem on the server itself.

    If anyone has the same issue or similar than check your opendir settings in your php.ini file. For me it was block the script, so reducing the tightness on where scripts could run it works perfect now.