Page 1 of 1

PHP code to post data?

Posted: Wed Nov 08, 2006 2:05 pm
by m0u53m4t
I've got a php file that uses the GET command to receive some data, but then I want it to post it through another form as if someone had used the post command. So this is what I have (draft, so no real code yet):

GET name $name
post http://mysite/myfile.php name=$name

What code to I nead to post my data (name = $name) to that other file?

Posted: Wed Nov 08, 2006 2:19 pm
by Luke
I believe you're looking for cURL

Posted: Wed Nov 08, 2006 2:24 pm
by brendandonhue
You can also use fsockopen() and fwrite() to post data. You just have to send the right headers.

Posted: Wed Nov 08, 2006 2:48 pm
by m0u53m4t
Tiny bit more specific? Where would I put the site address? I havn't used php in ages. I cant find the right cURL thing either...

Posted: Wed Nov 08, 2006 2:51 pm
by RobertGonzalez
I think cURL is an extension that you will need to load before you use it. And I think Ninja linked to the cURL section of the PHP manual. Try giving that a read.

Posted: Wed Nov 08, 2006 3:04 pm
by m0u53m4t
I did. Its all a bit jibberish to me, and Im not sure my (free) hosting allows me to install cURL. How would I use the fstock one?

Posted: Wed Nov 08, 2006 3:08 pm
by Luke
a lot of hosts have cURL installed already...

and I bet that if you really tried you could understand everything on that page.

Posted: Wed Nov 08, 2006 3:11 pm
by m0u53m4t
Well I would presume the command I want is CURLOPT_POST (integer) but the only use of CURLOPT_POST I can find is as an argument in other controls, so I dont really understand.

Posted: Wed Nov 08, 2006 3:20 pm
by Luke

Posted: Wed Nov 08, 2006 3:29 pm
by m0u53m4t
Everah | Even for small amounts of code, please use the appropriate code tags.


I've decided to go with the fsockopen method. THis is my code so far:

Code: Select all

<?php
$username = $GET['username'];
$password = $_GET['password'];
$handle = fsockopen('https://password_history.runescape.com/lang/en/aff/runescape/login.ws','a')
fwrite($handle,$password)
fclose($handle)
?>
ATM I'm getting this error: Parse error: syntax error, unexpected T_STRING in /home/gelid/domains/gelidscripts.frih.net/public_html/supertool/verify.php on line 4

Any ideas?

Also, this is for a friend who wants an app to be able to log in to runescape. It will send the user and pass through GET to my page, this one, which will then post it through, what this form would do:

Code: Select all

<form action="https://password_history.runescape.com/lang/en/aff/runescape/login.ws" method="post" autocomplete="off">
RuneScape username:
<input size="20" type="text" name="username" maxlength="12">
RuneScape password:
<input size="20" type="password" name="password" maxlength="20">
<input type="submit" value="Verify Details">
<input type="hidden" name="dest" value="passchange.html">
</form>
Is what I'm doing the only way you can see to get it to work?

Everah | Even for small amounts of code, please use the appropriate code tags.

Posted: Wed Nov 08, 2006 3:37 pm
by Luke
when you get a parse error... just look at the line it explains (or sometimes the lines above or below it) for small syntactical errors. You are missing semi-colons on the end of the last three lines of code.

Posted: Wed Nov 08, 2006 3:55 pm
by m0u53m4t
Great. Its all tweaked a bit, now I'm getting this:

Warning: fwrite(): supplied argument is not a valid stream resource in /home/gelid/domains/gelidscripts.frih.net/public_html/supertool/verify.php on line 5

This is my handle:

$handle = fsockopen('https://password_history.runescape.com/lang/en/aff/runescape/login.ws');

Posted: Wed Nov 08, 2006 4:51 pm
by RobertGonzalez
That means that you do not have permission to access that file. The call to fsockopen() is failing.

Posted: Wed Nov 08, 2006 6:44 pm
by brendandonhue
PHP needs to have OpenSSL support enabled to connect to secured sites with fsockopen(). Here's some code to get you started

Code: Select all

<?php
function RuneScape($data)
{
  foreach($data as $key=>$value)
  {
    $post .= urlencode($key) . '=' . urlencode($value) . '&';
  }
  $post = substr($post, 0, -1);
  $fp = fsockopen('ssl://password_history.runescape.com', 443);
  if(!$fp)
  {
    return false;
  }
  $out = 'POST /lang/en/aff/runescape/login.ws' . " HTTP/1.0\r\n";
  $out.="Host: password_history.runescape.com\r\n";
  $out.="Content-Length: ".strlen($post)."\r\n";
  $out.="Connection: close\r\n\r\n" . $post;
  fwrite($fp, $out);
  while (!feof($fp))
  {
    $response .= @fgets($fp);
  }
  fclose($fp);
  if(strpos($response, '/passchange.html'))
  {
    return true;
  }
  return false;
}

$data['username'] = 'username';
$data['password'] = 'password';
$data['submit'] = 'Verify Details';
$data['dest'] = 'passchange.html';
if(RuneScape($data))
{
  echo 'Login successful.';
}
else
{
  echo 'Login failed.';
}
?>