-5

I want to know how to post data with php, for example get the input from the local php then write to the input with id on another webpage. I think that those are called PHP bots, but I didn't quite understand the function. If you could just show me the code, if this is possible.

UPDATE: To make it cleaner - how to "write" to an input on another page with PHP.

Bogdan Cuza
  • 19
  • 1
  • 6
  • Do you mean you wish to make another request with the post data, or are you just trying to echo out post data that was sent to your page? – Kyle Feb 19 '13 at 14:03
  • Your anwser is here: http://stackoverflow.com/questions/13971003/php-server-side-post It even specifies specifically to call the POST directly from your PHP code (server-side). Client-side you can do it in means of a FORM-tag and specify your action="" field towards the webapge you want to POST to. –  Feb 19 '13 at 14:05
  • To make it clear, I'm a noob I don't understand it quickly – Bogdan Cuza Feb 19 '13 at 15:22
  • I also asked nicely not to close the question, **because** I need answers for my specific question, not other questions. – Bogdan Cuza Feb 19 '13 at 15:26

1 Answers1

3

To Post Data to an URL, you can use CURL for that.

$my_url = 'http://www.google.com';
$post_vars = 'postvar1=' . $postvar1 . '&postvar2=' . $postvar2;

$curl = curl_init($my_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);

UPDATE

If you don´t want to use CURL you can try it this way:

$server= 'www.someserver.com';
$url = '/path/to/file.php';
$content = 'name1=value1&name2=value2';
$content_length = strlen($content);
$headers= "POST $url HTTP/1.0\r\nContent-type: text/html\r\nHost: $server\r\nContent-length: $content_length\r\n\r\n";
$fp = fsockopen($server, $port, $errno, $errstr);
if (!$fp) return false;
fputs($fp, $headers);
fputs($fp, $content);
$ret = "";
while (!feof($fp)) {
$ret.= fgets($fp, 1024);
}
fclose($fp);
print $ret;

and if you´re using PHP5 here's a function you can use to post data to an URL:

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

this function is from here

and to use this function do something like

<?php

do_post_request('http://search.yahoo.com/', 'p=hello+yahoo')

?>

this will search for "hello yahoo"

makim
  • 3,154
  • 4
  • 30
  • 49
  • 1
    Careful, on a clean PHP install on Linux curl is not a default library that comes with the package. It should just be pointed out how you really POST in PHP. This is not a direct method. It's either by calling a PHP fopen()/redirect or in means of a HTML-form. –  Feb 19 '13 at 14:11
  • Could you explain it and give an example without using cURL? – Bogdan Cuza Feb 19 '13 at 15:22
  • look at my updated answer ;-) – makim Feb 19 '13 at 16:22
  • could you explain the second code? – Bogdan Cuza Feb 19 '13 at 19:15
  • I meant the second code, that's without cURL and second that do post, how to specify the input, if they're multiple and also get a captcha from the page – Bogdan Cuza Feb 20 '13 at 13:40
  • multiple input you can specifiy with "&" like "post1=hello&post2=world" and you have to put it in the $content variable! To get a captcha from the page you have to parse the response – makim Feb 20 '13 at 14:51
  • Hey, I wanted to ask if it is possible to do this with javascript? – Bogdan Cuza Apr 10 '13 at 16:52