PHP code to post data?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

PHP code to post data?

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I believe you're looking for cURL
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

You can also use fsockopen() and fwrite() to post data. You just have to send the right headers.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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');
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That means that you do not have permission to access that file. The call to fsockopen() is failing.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post 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.';
}
?>
Post Reply