Solved

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

Posted on 2004-08-16
5
456 Views
Last Modified: 2009-07-29
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
Comment
Question by:abradley81
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 50 total points
ID: 11812768
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
 

Author Comment

by:abradley81
ID: 11813073
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
 
LVL 48

Expert Comment

by:hernst42
ID: 11813436
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
 

Author Comment

by:abradley81
ID: 11813845
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
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 50 total points
ID: 11814066
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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question