Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-16
5
Medium Priority
?
470 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 150 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 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

971 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