4

I have a PHP script that connects to an api and posts information to their systems, but when its trying to connect it throws a fatal 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 send it to a PHP script which just grabs the IP then it works, but if I send it to the API it doesn't. My PHP script creates XML and then forwards to the server. 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; 

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.

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)

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 
2
  • I would suggest that it has something to do with the arrays that you are using, first test if that key exists, then use it. Commented Jul 22, 2009 at 13:11
  • smoove666 yes it works fine, but its a closed server so can you delete reference to it? I can access it fine with a browser,I can also access it with a script and parse the response it provides. Commented Jul 22, 2009 at 13:30

5 Answers 5

1

I would suggest using curl instead of fopen(). curl is both more flexible, and more secure. Many servers disable allow_url_fopen, curl also fails more gracefully when there are problems on the remote server.

Sign up to request clarification or add additional context in comments.

2 Comments

Have not used Curl much, any pointers on which functions to use?
There's a pretty good sample that was posted in June on the example page at php.net: us3.php.net/manual/en/curl.examples.php
1

I just tested the URL in my browser and got a connection error.

Maybe the problem is with their server (or you have the wrong URL)?

Edit:

Looks like ther server is throwing a 500 error - maybe because the data you're posting is invalid.

You could use a packet sniffer and check the exact data you're sending to the server (or use cURL as suggested by @acrosman)

3 Comments

The guy just removed the real url and put a false one so nobody would access it. "apitestserver.co.uk" should have been clear enough ;)
yeah apitestserver is a falsie as the original one is private and locked behind a firewall so no one would be able to see it anyway! But I can access it fine, from the server, as my update says.
Yes well spotted, thats because I am not sending the correct data to the API. I will update my main thread with the errors from the main script.
1

apitestserver.co.uk doesn't respond to ping and the url is inaccessable. The API-server doesn't seem to be up.

2 Comments

Its a closed test server, you wont be able to access it without the firewall letting you through. Which it wont, but also its a closed server so could you delete reference to it please :) I thought I had replaced all instances in the original post DOH
Note I can access it fine, and i can also access it with a simple script which opens, reads and prints the xml the api returns as default.
0

or You can change the headers:

$optional_headers = 'Content-Type: application/json') 

Comments

0

First error message suggests problems with apitestserver - it returns error code 500, which states for "Internal server error".

Second one informs that PHP can't resolve host name into IP address - I'd look into DNS configuration and/or hosts file.


Apparently 'fopen' function does not return proper stream. There can be many possibilities when that happens, but according to PHP manual, fopen function on fail should present E_WARNING message with proper commentary. It is possible that you have errors of that class silenced - call error_reporting(E_ALL) at the beginning of your script and look for some relevant error messages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.