Jump to content

[SOLVED] http response from post to a foreign website - cross domain posting results


mrfitz

Recommended Posts

:-\

    I have made a php script that is supposed to take a $_POST input from a different website and then parse the data and send a response back to the sender. There will be many senders who do $_POST 's to the script but I cant figure out a couple of things.

    1. when the form is submitted by the client this is what the form action line looks like:

    <form action="http://xxxxx.com/yyyyy/zzzz.php" method="post"> Of course I have checked to make sure that the URL is correct, and it is.

    2. The response from that post is supposed to be just a comma delimited string of data. Like:

    "39.00,2009-06-19,1,noerr;" The problem is I don't know how to "send" the response back to the client.

    I have tried quite a few things, but because the "request" comes from outside of the form action's domain, it will come from 1 of several "Outside" domains, I am having problems getting the response, and I am not even sure that the posts are getting received

    by the form action's domain......

 

    I have done tons of php programming but have never tried anything across domains so I am stumped!! Could someone, smarter that I please help????

    Thanks

    Mrfitz

You sure can receive $_POST from other domains, you only have to change the action on the form.

 

Sending $_POST without submitting is another thing, it sure is possible but it will require some skills with PHP and sockets. You can view a tutorial here: http://www.alt-php-faq.org/local/55/#id55.

You sure can receive $_POST from other domains, you only have to change the action on the form.

 

Sending $_POST without submitting is another thing, it sure is possible but it will require some skills with PHP and sockets. You can view a tutorial here: http://www.alt-php-faq.org/local/55/#id55.

 

Thanks unska,

I checked out the code from the site referenced, but it poses questions, but it puts me on the right track. 

<?

// Generate the request header

$ReqHeader =

"POST $URI HTTP/1.1\n".

"Host: $Host\n".

"Content-Type: application/x-www-form-urlencoded\n".

"Content-Length: $ContentLength\n\n".

"$ReqBody\n";

 

// 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++] = fgets($socket, 128);

}

//-------------------------------------------

?>

Above is the snip...

Questions...

[*]$ReqBody is referenced but it is NEVER used, I assume it contains my $_POST data! But I guess that I have to "fill it up".  But when/where is it output?

[*]I'm also assuming the $Result[$idx++] will contain my results! Is that correct?

But, other than that this certainly puts me on course.

Thanks for YOUR fast response.

Mr Fitz

OK, I looked at the referred site and decided to use CURL to solve my problems. Here is a snip.

<?

$URL="www.mysite.com/test.php";

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,"https://$URL");

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");curl_exec ($ch);   

curl_close ($ch);

?>

That worked great and was easy to implement as well as allowing the headers to go to a file in the subfolder and directly to a filename other than "INDEX.PHP".

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.