For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2006 > php POST









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author php POST
Peter

2006-12-11, 7:01 pm

instead of building a URL, e.g.:
domain.com?a=1&b=2&c=3

Is it possible to POST from inside a php script ? (without a form and a
button a user has to click) ?

E.g. from inside the php file a post is done to domain.com
and the parameters a,b and c are passed along.


Simon Harris

2006-12-11, 7:01 pm

"Peter" <Will_Bounce_So_Use_My_First_Name@Smart-Projects.net> wrote in
message news:B2Gdh.223306$S45.4841544@phobos.telenet-ops.be...
> instead of building a URL, e.g.:
> domain.com?a=1&b=2&c=3
>
> Is it possible to POST from inside a php script ? (without a form and a
> button a user has to click) ?


Hi Peter, a quick google search found this, hope its what your looking for.
http://www.faqts.com/knowledge_base...id/12039/fid/51

Simon.

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 815 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Rik

2006-12-11, 7:01 pm

Simon Harris wrote:
> "Peter" <Will_Bounce_So_Use_My_First_Name@Smart-Projects.net> wrote in
> message news:B2Gdh.223306$S45.4841544@phobos.telenet-ops.be...
>
> Hi Peter, a quick google search found this, hope its what your
> looking for.
> http://www.faqts.com/knowledge_base...id/12039/fid/51


Simon, a sig seperator is: "-- \n" (hyphen hyphen space newline). That way
people replying to you won't have to strip your sig manually.

To answer the question: fsockopen() can be used if all else fails, but is
very cumbersome/tedious.
That's why we have cURL (if it's available on your server):

$postarray = array('key1'=>'value1','key2'=>'value2');
$ch = curl_init('http://example.com');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responce = curl_exec($ch);

Even (local) files can be posted like this without any problems.

Grtz,
--
Rik Wasmus


-Lost

2006-12-11, 7:01 pm

"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:60b49$457756cf$8259c69c$19407@news2
.tudelft.nl...
> Simon Harris wrote:
>
> Simon, a sig seperator is: "-- \n" (hyphen hyphen space newline). That way
> people replying to you won't have to strip your sig manually.
>
> To answer the question: fsockopen() can be used if all else fails, but is
> very cumbersome/tedious.
> That's why we have cURL (if it's available on your server):
>
> $postarray = array('key1'=>'value1','key2'=>'value2');
> $ch = curl_init('http://example.com');
> curl_setopt($ch,CURLOPT_POST, true);
> curl_setopt($ch,CURLOPT_POSTFIELDS,$post
);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> $responce = curl_exec($ch);
>
> Even (local) files can be posted like this without any problems.
>
> Grtz,
> --
> Rik Wasmus


I would be interested in a static example of this as I cannot reproduce it.

Once, I had output. It wound up showing me a (print_r) print fifty or so times before I
had to restart the server. The oddest thing is I have gotten mixed results with it
delivering my 404 page and even printing then showing 500 internal server.

-Lost


Simon Harris

2006-12-11, 7:01 pm

> Simon, a sig seperator is: "-- \n" (hyphen hyphen space newline). That way
> people replying to you won't have to strip your sig manually.


Thanks, but I know, what you are seeing is added automatically by my
anti-spam software, its not my sig.

Simon.

--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 859 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Sebastian 'CrashandDie' Lauwers

2006-12-11, 7:01 pm

Simon Harris wrote:

> Thanks, but I know, what you are seeing is added automatically by my
> anti-spam software, its not my sig.


Then just add an empty signature, make sure there's a sig delimiter just
above the spam your anti-spam spams :)

Or change software...

> Simon.


S.
Michael Fesser

2006-12-11, 7:01 pm

..oO(Simon Harris)

>
>Thanks, but I know, what you are seeing is added automatically by my
>anti-spam software, its not my sig.


Kinda broken, isn't it? Why do you use it at all for outgoing posts?

Micha
Simon Harris

2006-12-11, 7:01 pm


"Sebastian 'CrashandDie' Lauwers" <crashanddie+news@gmail.com> wrote in
message news:45783470$0$11508$426a74cc@news.free.fr...
> Simon Harris wrote:
>
>
> Then just add an empty signature, make sure there's a sig delimiter just
> above the spam your anti-spam spams :)

Will do!

> Or change software...

No thanks! (Its free, and works very well!)



--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 914 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Simon Harris

2006-12-11, 7:01 pm

> Kinda broken, isn't it? Why do you use it at all for outgoing posts?
Quote: "what you are seeing is added automatically by my anti-spam software"


--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 914 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!


Rik

2006-12-11, 7:01 pm

-Lost wrote:
> I would be interested in a static example of this as I cannot reproduce it.
>
> Once, I had output. It wound up showing me a (print_r) print fifty or so times before I
> had to restart the server. The oddest thing is I have gotten mixed results with it
> delivering my 404 page and even printing then showing 500 internal server.


Quick'n'Dirty cURL example:
(For these purposes, I've put eveything in localhost/test, any other url
is offcourse possible).

-----form.php-----------
<?php
if(isset($_POST['submit'])){
echo "<pre>Victory!\n",var_dump($_POST),"</pre>";
} else {
?>
<form method="post" action ="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1"><br />
<input type="text" name="field2"><br />
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
------------------------
-----curl.php-----------
<?php
$postarray = array(
'submit' => 'submit',
'field1' => 'First field',
'field2' => 'Second field');
//change following url to location of your form.php
$ch = curl_init('http://localhost/test/form.php');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post
array);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,t
rue);
print curl_exec($ch);
?>
------------------------

Keep in mind that in some external forms/server outside your control (or
on your own server if you've set it), you may need more curl_setopt()'s
for for instance sessions, enabling redirects, timeout's, user-agent
headers, authorisation etc...

The only problem I have with posting forms to external servers is now
those few rare breeds which require an explicitly set variable by
javascript... Which offcourse is Evil.

Quick example for file-upload:
----form.php----------------
<?php
if(isset($_POST['submit'])){
echo "<pre>Victory!\n",var_dump($_POST),"</pre>";
echo "<pre>Fileinfo:\n",var_dump($_FILES),"</pre>";
} else {
?>
<form method="post" action ="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1"><br />
<input type="text" name="field2"><br />
<input type="file" name="file"><br />
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
-----------------------------
----curl.php-----------------
<?php
$postarray = array(
'submit' => 'submit',
'field1' => 'First field',
'field2' => 'Second field');

// Change following url to location of your form.php
$ch = curl_init('http://localhost/test/form.php');

// Change following filename to an actual file on
// your server running curl

// If curl doesn't respond or output is empty, most
// likely there's a problem getting the file

$filename = dirname(__FILE__).'/files/IMG_0182.JPG';
$postarray['file'] = '@'.$filename;

curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post
array);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,t
rue);
print curl_exec($ch);
?>
-----------------------------

Output of cURL in my case:
-----------------------------
<pre>Victory!
array(3) {
["submit"]=>
string(6) "submit"
["field1"]=>
string(11) "First field"
["field2"]=>
string(12) "Second field"
}
</pre><pre>Fileinfo:
array(1) {
["file"]=>
array(5) {
["name"]=>
string(12) "IMG_0182.JPG"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(37) "C:\Program Files\xampp\tmp\php212.tmp"
["error"]=>
int(0)
["size"]=>
int(645836)
}
}
</pre>
-------------------------------

The main problem in cURL is not the settings & using it, but
guessing/determening what requirements an outside form has.

When encountering problems with an external form, I recommend checking
the headers your actual browser sents (Fiddler can capture this for
MSIE, liveHTTPheaders can do this for Firefox). Try to set all headers
exactly alike in cURL untill it works, and then remove them again one by
one to see which are actually required.
--
Rik Wasmus
-Lost

2006-12-11, 7:01 pm

"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:9dc22$4579e572$8259c54a$29129@news2
.tudelft.nl...

"TONS of useful cURL information for poor ole' Lost"

Thanks, Rik!

-Lost


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com