Solved

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

Posted on 2004-08-16
5
448 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now