Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is this php script wriiten correctly? fsockopen error message:

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
US
I have a form on my website that creates cookies in the header when the page loads. I am attempting to mimic a form post.

I get the following 2 errors when tring to simulate the post:

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/ on line 60

Warning: fsockopen(): unable to connect to http::80 in /var/ on line 60
Error 0 Success

The initial code on my page with the data I want to automatically send to my form is

Code:
include("postit2.php");

require_once "HTTP/Request.php";

$req =& new HTTP_Request(" [URL unfurl="true"]http://www.abc.com/page.php");[/URL]


$data["dbType"] = "mysql";
  $data["dbHost"] = "localhost";
  $data["dbUser"] = "xxx";
  $data["dbPass"] = "letmein";
  $data["dbName"] = "me";

  $result = post_it2($data, " [URL unfurl="true"]http://www.abc.com/page.php");[/URL]

  if (isset($result["errno"])) { 
    $errno = $result["errno"]; 
    $errstr = $result["errstr"]; 
    echo "<B>Error $errno</B> $errstr"; 
    exit; 
  } else { 

    for($i=0;$i< count($result); $i++) echo $result[$i]; 

  } 

?>

the php file with the function info that should submit the form is:

Code:
<?php

function post_it2($data, $URL) {

//  Strip http:// from the URL if present
    $URL = ereg_replace("^[URL unfurl="true"]http://",[/URL] "", $URL);

//  Separate into Host and URI
    $Host = substr($URL, 0, strpos($URL, "/"));
    $URI = strstr($URL, "/");

//  Form up the request body
    $ReqBody = "";
    while (list($key, $val) = each($data)) {
      if ($ReqBody) $ReqBody.= "&";
      $ReqBody.= $key."=".urlencode($val);
    }
    $ContentLength = strlen($ReqBody);

//  Generate the request header
    $ReqHeader =
      "POST $URI HTTP/1.0\n".
      "Host: $Host\n".
      "User-Agent: PostIt\n".
      "Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded\n".[/URL]
      "Content-Length: $ContentLength\n\n".
      "$ReqBody\n";

//     echo $ReqHeader;


//  Open the connection to the host
    $socket = fsockopen($Host, 80, &$errno, &$errstr);
    if (!$socket) {
      $Result["errno"] = $errno;
      $Result["errstr"] = $errstr;
      return $Result;
    }
    $idx = 0;
    fputs($socket, $ReqHeader);
    while (!feof($socket) && $Result[$idx-1] != "0\r\n") {
    if (substr($Result[$idx-1], 0, 2) == "0\r\n") echo "The End:".strlen($Result[$idx-1]);
      $Result[$idx++] = fgets($socket, 128);
    }
    return $Result;
  }
?>

why do I get the two errors above? Is it because I am suppose to send the cookies back in the form somehow?

The name of the cookie is PHPSESSID I believe

I have read an article on zend dot com about mimicking submission posts and am still lost. Any help given would be greatly appreciated.

Thanks

[URL unfurl="true"]http://www.zend.com/zend/spotlight/mimocsumissions.php?out=dynamicwebpages [/url]
 
Maybe this is it:
Your regular expression looks for http:// at the beginning of the line (/^http)
However, the function call is passed
Code:
$req =& new HTTP_Request(" [URL unfurl="true"]http://www.abc.com/page.php");[/URL]
which begins with a string. The substitution fails.
Hence, the error message:
unable to connect to http::80
 
Sorry - I said 'string' and meant 'space'.
 
I didnt know i was suppossed to remove '$host' and replace it with something else because of a line above in the code:

Code:
$Host = substr($URL, 0, strpos($URL, "/"));

i was under the impression that that line would place teh site address automatically?

Do i need to put the site address there myself?
 
Please read the posts more carefully.
This is what fails:
Code:
//  Strip http:// from the URL if present
    $URL = ereg_replace("^[URL unfurl="true"]http://",[/URL] "", $URL);
It is assumed that the string actually starts with http:// but yours does not - it starts with a SPACE.

Change the parameter you pass and remove the space. That should resolve the issue.
 
can i also resolve this by deleting the space between the quotation mark & the 'h' in http?

from
Code:
$req =& new HTTP_Request(" [URL unfurl="true"]http://www.abc.com/page.php");[/URL]

to

Code:
$req =& new HTTP_Request("[URL unfurl="true"]http://www.abc.com/page.php");[/URL]
 
now i get :

Fatal error: Call to a member function on a non-object in /var/ on line 17

Code:
<?php 

  include ("HTTP/Request.php");

include("postit2.php");

require_once "HTTP/Request.php";

$req =& new HTTP_Request(" [URL unfurl="true"]http://www.abc.com/page.php");[/URL]



$response = $req->sendRequest();

if (PEAR::isError($response)) {
    echo $response->getMessage();

last line is line 17
 
My first guess is that $req->sendRequest() returned something other than an object. Maybe the value FALSE?

It could be due to the fact that your code still has that spurious space at the beginning of your URL in the line:

$req =& new HTTP_Request("

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sorry, i did take the space out, i just copied and pasted from my above post.
 
when run by itself [removing rest of code]it returns:

Array ( [0] => Array ( [expires] => [domain] => [path] => / [secure] => [name] => PHPSESSID [value] => 69d94ac6f601d8b8e63e4e3d82233ab5 ) )
 
when run by itself [removing rest of code]it returns:

Array ( [0] => Array ( [expires] => [domain] => [path] => / [secure] => [name] => PHPSESSID [value] => 69d94ac6f601d8b8e63e4e3d82233ab5 ) )

Code:
<?php 

  include ("HTTP/Request.php");

include("postit2.php");

require_once "HTTP/Request.php";

$req =& new HTTP_Request("[URL unfurl="true"]http://www.abc.com/page.php");[/URL]



$response = $req->sendRequest();

if (PEAR::isError($response)) {
    echo $response->getMessage();
} else {
    print_r($response->getResponseCookies());
}
?>
 
not being able to edit my posts makes it difficult:

the correct code is below

when run by itself [removing rest of code]it returns:

Array ( [0] => Array ( [expires] => [domain] => [path] => / [secure] => [name] => PHPSESSID [value] => 69d94ac6f601d8b8e63e4e3d82233ab5 ) )

Code:
<?php 

 require_once "HTTP/Request.php";

include("postit2.php");



$req =& new HTTP_Request("[URL unfurl="true"]http://www.abc.com/page.php");[/URL]


$response = $req->sendRequest();

if (PEAR::isError($response)) {
    echo $response->getMessage();
} else {
    print_r($req->getResponseCookies());
}
?>
 
Would you know how i can reurn the cookie to the form with ther rest of the data i am sending?

I have browsed through some PEAR HTTP_REQUEST material but found nothing on retuning cookies?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top