Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 317939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:31:25+00:00 2026-05-12T08:31:25+00:00

I have a PHP script that connects to an api and posts information to

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:31:26+00:00Added an answer on May 12, 2026 at 8:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 213k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You will need Path.GetFullPath() + case insensitive string comparison. Running… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer I'd suggest using the ajaxOptions to the tabs() plugin. I… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Take a look at question: Java API to find out… May 12, 2026 at 10:28 pm

Related Questions

I am a complete novice at php and postgres. I have been searching all
I am sure there is a simple answer to this, but I am drawing
I'm wondering what is the quickest and most reliable way to forward mail from
I have got this JavaScript code for uploading files to my server (named it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.