-2

I'm currently trying to query an API system (the return being XML), and everything works fine when I use file_get_contents with the URL:

http://bcoddsfeed.goldenpalace.be/getXML.php

However, after I find a game ID and add ?gameid=ID to the URL, I get a very odd error (which appears to prevent me from turning it into multi-level array by means of json_encode and json_decode). An example of the URL which does not work is:

http://bcoddsfeed.goldenpalace.be/getXML.php?gameid=1130733827

When I use var_dump on the data I've retrieved, I get it back in a somewhat mangled format with the following HTTP_USER_AGENT error at the beginning, which mucks things up.

Notice: Undefined index: HTTP_USER_AGENT in /usr/local/www/bcoddsfeed.goldenpalace.be/getXML.php on line 29

The full output looks something like:

string(6502) "
Notice: Undefined index: HTTP_USER_AGENT in /usr/local/www/bcoddsfeed.goldenpalace.be/getXML.php on line 29
2015-01-18 10:00:00Match ResultW24.6W11.81X3.2<_1X12X2>Double Chance1X1.13121.27X21.88Total Goals: Over/UnderTotal Goals Under | 5.51.01Total Goals Over | 5.511Total Goals: Over/UnderTotal Goals Under | 3.51.15Total Goals Over | 3.54.5Total Goals: Over/UnderTotal Goals Under | 14.95Total Goals Over | 11.12Total Goals: Over/UnderTotal Goals Under | 31.22Total Goals Over | 33.75Total Goals: Over/UnderTotal Goals Over | 21.82Total Goals Under | 21.88Total Goals: Over/UnderTotal Goals Under | 0.56.75Total Goals Over | 0.51.07Total Goals: Over/UnderTotal Goals Over | 4.510Total Goals: Over/UnderTotal Goals Under | 41.05Total Goals Over | 47.5Total Goals: Over/UnderTotal Goals Over | 1.51.45Total Goals Under | 1.52.5Total Goals: Over/UnderTotal Goals Under | 2.51.5Total Goals Over | 2.52.4Goals of Team 2Goals of Team 2 Over | 2.511Goals of Team 2 Under | 2.51.01Goals of Team 1Goals of Team 1 Under | 21.25Goals of Team 1 Over | 23.5Goals of Team 2Goals of Team 2 Over | 12.85Goals of Team 2 Under | 11.35Goals of Team 2Goals of Team 2 Over | 1.54.5Goals of Team 2 Under | 1.51.15Goals of Team 1Goals of Team 1 Under | 1.51.6Goals of Team 1 Over | 1.52.15Goals of Team 1Goals of Team 1 Over | 11.45Goals of Team 1 Under | 12.5Goals of Team 1Goals of Team 1 Under | 2.51.15Goals of Team 1 Over | 2.54.5Asian HandicapAsian Handicap 2 | 2.51.05Asian Handicap 1 | -2.57.5Asian HandicapAsian Handicap 2 | 1.51.27Asian Handicap 1 | -1.53.3Asian HandicapAsian Handicap 1 | -25.5Asian Handicap 2 | 21.1Asian HandicapAsian Handicap 2 | 11.45Asian Handicap 1 | -12.5Asian HandicapAsian Handicap 2 | 03.05Asian Handicap 1 | 01.32Correct Score2:07.41:05.20:2231:2161:41602:41806:02105:1955:21902:193:014.53:118.53:2420:071:16.42:2210:19.46:1211:3500:3653:31002:3604:1404:0344:31804:2900:41804:42303:42705:075"

Does anyone have any ideas on how to fix this HTTP_USER_AGENT mishap and get a nice clean xml string which I can convert into a .json for use in my php application? I'm very unexperienced with this, and any help would be greatly appreciated since I am totally lost.

I don't see this error when I visit the link with my browser, so I'm not sure what's going on.

Thank you!

I've also tried using the curl function below to call in the data via the URL, but no luck. I still get the same error.

CURL FUNCTION

    public static function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
muffinjello
  • 158
  • 1
  • 5
  • 16

1 Answers1

1

Looks as if this “API” is just poorly written. It expects each and every request made to it to include a User-Agent request header – and fails to consider that not every client might send one. (Your browser usually does, which is why you don’t experience that error when you visit that URL in your browser, but your PHP script apparently does not.)

Now this API just tries to access this value without checking whether it actually exist; since it seems to be written in PHP, most likely via $_SERVER['HTTP_USER_AGENT'] – and if the client did not send such a header, it generates the PHP notice about an undefined index.

To actually fix this would be the job of whoever cobbled this API together. If they demand a User-Agent header to be send with the request, then their script should check whether such a value is set, and fail “gracefully” if it isn’t, f.e. by returning some kind of error message as XML.

As a workaround, you can make file_get_contents send a User-Agent header in your script.
Setting a value for the user_agent configuration value at runtime using ini_set should suffice. Whether or not you need to send a specific User-Agent (one like a common browser would send), or if just any value is enough, you have to try and see. Most likely any value will do.

If that doesn’t work, or you need to send any additional headers, then please refer to this SO question: PHP file_get_contents() and headers


Edit: Using cURL of course you got the same problem, unless you specify a User-Agent so that this API will not fail. You can do that using curl_setopt and the CURLOPT_USERAGENT option.

Community
  • 1
  • 1
CBroe
  • 91,630
  • 14
  • 92
  • 150