Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

Using fsockopen() to connect to a URL and POST data

My goal is to pass data to the next page in the same way a form POST would, just without having to actually press the button. I know you can use cURL, but i'd rather stick to fsockopen. It seems easy enough, but I can't seem to get it to work. I know for a fact I'm using the correct URL and index.php is a working file. After I run the code below I get this error:

---------ERROR--------
Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in c:\inetpub\wwwroot\manage\add.php on line 13

Warning: fsockopen(): unable to connect to localhost/index.php:80 in c:\inetpub\wwwroot\manage\add.php on line 13
ERROR: 0 - The operation completed successfully.


----------CODE-----------
SendPost("index.php");

function SendPost($url) {  
      $fp = fsockopen($_SERVER['HTTP_HOST']."/".$url, 80, $errno, $errstr);
      if (!$fp) {
         echo "ERROR: $errno - $errstr<br />\n";
      } else {
         fwrite($fp, "\n");
         echo fread($fp, 26);
         fclose($fp);
      }
}
--------------
What am I doing wrong. I'm using windows XP and would like to get it running on my localhost before using it on my live server.

Since I get the error "ERROR: 0 - The operation completed successfully", php.net says it has something to do with "an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket."
http://us2.php.net/manual/en/function.fsockopen.php

Any answers to what I'm doing wrong????
0
abradley81
Asked:
abradley81
  • 3
  • 2
2 Solutions
 
hernst42Commented:
You are trying to connect to host:

$_SERVER['HTTP_HOST']."/".$url
=> eg. "www.example.com/url/you/access.php"
That part ."/".$url is too much to define a hostname

That is no valid hostname. I think the function that you want to use is
fopen($_SERVER['HTTP_HOST']."/".$url) to get the content of that url.
0
 
abradley81Author Commented:
How do I POST information to the next page? The same way the form post works, but being able to do it in php? Am I on the wrong track with fsockopen?
0
 
hernst42Commented:
no you can do it with fsockopen, but you need to emulate the complete HTTP-Request

how this can be done see http://de.php.net/fsockopen#37517
0
 
abradley81Author Commented:
Ok, I found this code and it looks like what I want. But it doesn't go to the next page, it just goes to the same page I'm on?? Why doesn't it go to the next page?
-------------
$postdata = http_post('localhost', '/provider_add2.php', 'varible=data&varible2=data2');

function http_post($path, $data) {
     $host = "localhost";
     $path = "/OutSource/".$path;
     $http_response = "";
     $content_length = strlen($data);

     $fp = fsockopen($host, 80);
     fputs($fp, "POST $path HTTP/1.1\r\n");
     fputs($fp, "Host: $host\r\n");
     fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
     fputs($fp, "Content-Length: $content_length\r\n");
     fputs($fp, "Connection: close\r\n\r\n");
     fputs($fp, $data);
     while (!feof($fp))
     {
          $http_response .= fgets($fp, 128);
     }
     fclose($fp);
     return $http_response;
}
0
 
hernst42Commented:
The URL in the browser is the same, but the content is from the page you have postetd.

If I understand you correctly you want to empulate a submit of a form by PHP as a Browser would have done it and the Browser should think the form was submited by him?? This is not possible I think. You could use a Location-Header but that does not work with POST.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now