Solved

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

Posted on 2004-08-16
5
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

626 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