Richard Yates Posted November 12, 2011 Share Posted November 12, 2011 Trying to hook up to an API using php. The API commands are sent in url query strings. Depending on the command you are sending in the query string, the API returns an XML file or text. For commands that return an XML file I can use $response=simplexml_load_file($APIcall) and parse and store the xml that comes back into $response. But with text responses from the API I cannot find a way to handle the reply. Using xml_load_file() results in errors displayed since it was expecting xml and nothing gets stored in $response, although the command is correctly executed by the API. I can use headers('Location: $APIcall') but that just gets the text response sent to a new window. $response=get_headers($APIcall) works for some commands but still does not capture the text response. Quote Link to comment https://forums.phpfreaks.com/topic/251002-php-and-api-call/ Share on other sites More sharing options...
kicken Posted November 12, 2011 Share Posted November 12, 2011 I'm assuming $APIcall is the URL that you need to access and then get the results? Use file_get_contents() to access the URL and download the results (regardless of if it is text or xml). If it is an XML response, then do further processing with simplexml_load_string(). Quote Link to comment https://forums.phpfreaks.com/topic/251002-php-and-api-call/#findComment-1287625 Share on other sites More sharing options...
Richard Yates Posted November 12, 2011 Author Share Posted November 12, 2011 Thanks for the quick reply, kicken. Yes, $APIcall is the url used to access the API. Using file_get_contents() gives me a "HTTP request failed! HTTP/1.1 400 Bad Request" and the command is not executed by the API. Quote Link to comment https://forums.phpfreaks.com/topic/251002-php-and-api-call/#findComment-1287633 Share on other sites More sharing options...
Richard Yates Posted November 15, 2011 Author Share Posted November 15, 2011 Turns out the Bad Request error that i was getting using get_headers() was because there were spaces in the query string. Replacing those with %20 fixed that problem so I do not need to use file_get_contents(). Problem solved (mostly). Quote Link to comment https://forums.phpfreaks.com/topic/251002-php-and-api-call/#findComment-1288340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.