I have a php script that connects to an api and posts our information to their systems, but when its trying to connect it throws an exception error, and I cant for the life of me work out why.
Whats interesting is that if I send it to a simple php script which just grabs the IP then it works, but if I send it to the API it doesnt :(
The script is a big file, which creates xml and then forwards to the server, however I was getting errors so I just created the following smaller script purely to test the connection.
function do_http_request($url, $data, $method = 'POST', $optional_headers = 'Content-Type: application/atom+xml') {
$params = array(
'http' => array(
'method' => $method,
'content' => $data
)
);
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
$metaData = stream_get_meta_data($fp);
fclose($fp);
if(!preg_match('~^HTTP.*([0-9]{3})~', $metaData['wrapper_data'][0], $matches)) {
throw new Exception('MALFORED RESPONSE - COULD NOT UNDERSTAND HTTP CODE');
}
if (substr($matches[1], 0, 1) != '2') {
throw new Exception('SERVER REPORTED HTTP ERROR ' . $matches[1]);
}
return $response;
}
$data = 'hello';
$paul = do_http_request('http://apitestserver.co.uk:9000/Service.svc/Items',$data);
echo $paul;
This results in the following error
Fatal error: Uncaught exception 'Exception' with message 'Problem with http://apitestserver.co.uk:9000/Service.svc/Items' in /var/www/html/e/connect_test.php:17 Stack trace: #0 /var/www/html/e/connect_test.php(39): do_http_request('http://apitest....', 'hello') #1 {main} thrown in /var/www/html/e/connect_test.php on line 17
If I change the url to a simple script on another one of our servers which just grabs the IP of the incoming connection and returns it
$ip=$_SERVER['REMOTE_ADDR'];
echo 'IP equals = ' . $ip;
Then it works fine :( with no errors
Any thoughts?
Update -
with errors on it throws the following warning, probably because the script is not sending the correct info to the API
Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 500 Data at the root level is invalid. Line 1, position 1.
Also note that I can access the api server fine using fiddler to send manually created items across, its just when this script tries to connect and send data there is an issue. I wrote another quick script which connects and prints out the default response (an rss feed of submitted items)
UPDATE 2
When I run the 'main' connector script it throws the following two errors
Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/e/sequence.php on line 65
Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items) [function.fopen]: failed to open stream: Operation now in progress in /var/www/html/e/sequence.php on line 65