Click to See Complete Forum and Search --> : help with curl !


patimages
12-22-2006, 05:17 PM
Hi,

I am new at php and what I would like to achive is easy (in principle): I want to post 1 variable from one page to an other page and get it for further processing (kind of $var1 = $_POST['var1'];).
I found the following script but I am too dumb to understand how it works even reading around.
Could anyone help me by giving me (and others I guess : I saw that I am not alone) a small exemple on how to post using curl a single variable from page1 and get it on page2 (a print from page2 would be the easiest to make it clear).
thanks a lot in advance !

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/blah.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=ppp");
curl_exec ($ch);
curl_close ($ch);

coppocks
12-22-2006, 06:33 PM
First of all, curl is not by default installed as part of PHP. It is an external library that has to be installed separately on the server. And some web hosts may or may not have it available. Having said that, why not just use the Session Handling Functions that are native to PHP?

Using a normal form post method on one page, you can set a $_SESSION variable that will be available to you on any page that you call it from. It's simpler than curl if that's all you are trying to achieve.

For example using this simple form:

<form method="Post" action="subpage.php">
<input type="text" name="YourText">
<input type="submit" name="cmd" value="Submit"
</form>

... you can set the value of "YourText" in subPage.php like this:

<?php
session_start();
$_SESSION['FormText'] = $_POST['YourText'];
// ...
?>

... and then you can retrieve and use the session on any page you need it as long as the user is on your site... do the following on any page...

<?php
session_start();
echo $_SESSION['FormText'];
// Now do what ya gotta do with it
?>

Just be sure to set session_start(); at the beginning of every page that you are going to use sessions. If you keep this simple, it should work for you.

Check out the Session Handling Functions in the PHP manual for more detailed options.

NightShift58
12-22-2006, 07:24 PM
Patimages,

Are you trying to post from one page to another WITH or WITHOUT user interaction?

patimages
12-23-2006, 10:22 AM
without therefore no need for forms !

NightShift58
12-23-2006, 10:59 AM
Your code is almost there... $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/blah.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=ppp");
curl_exec ($ch);
curl_close ($ch);That will call the target page and POST variable as intended. Everything on the page "blah.php" will run as programmed, however, the user will see none of the resulting output.

For that, you need to set a cURL option, which will enable you to capture the output of "blah.php" in a variable, which you can then print/echo to screen. Your code would then look like this: $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/blah.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=ppp");

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);

curl_close ($ch);
print $result;
For more examples, see http://curl.haxx.se/libcurl/php/examples/

yitzle
12-23-2006, 09:37 PM
Whadya know!
I'm trying to do something similar. Is there any way to send an HTTP POST.
I was considering using cron in Unix with curl, but does PHP have any built in functions that do something similar?

NightShift58
12-23-2006, 09:46 PM
Unlike Cold Fusion, PHP doesn't have a built-in CRON-like facility. But you can write a PHP script and have it called by your own CRON job (maybe using WGET) or alternatively, use one of the so-called remote CRON services available on the net. (See http://www.google.com/search?q=remote+cron )

yitzle
12-23-2006, 09:53 PM
OK. My mistake. That was badly worded.
I'm, not asking about cron.
How can I use PHP to send a HTTP POST using the built in stuff (ie no curl; is wget built in? Is there something else?)

NightShift58
12-23-2006, 10:01 PM
If based on user interaction, you would obviously use a <form> to do that.

If you want to generate a non-interactive POST, you can use cURL - which is "usually" built in (but check with your host). WGET, on the other hand, isn't.

yitzle
12-23-2006, 10:16 PM
OK. curl is set up on my server. Will use it. Thanks!

NightShift58
12-23-2006, 11:10 PM
You're welcome. Happy holidays!