PHP Post data to url with curl

I know you will need sometimes to use a php code to send data (text) to an url using POST method, just like it was sent by a form.
So all you have to do now, is to copy this function and use it, you have to specify the url and the data to post and it goes !

<?php
function post_content($url,$nfields,$fields_string)  
{
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch,CURLOPT_POST,$nfields);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');  
    ob_start();  
    curl_exec ($ch);  
    curl_close ($ch);  
    $string = ob_get_contents();  
    ob_end_clean();  
    return $string;      
}
?>

How this will work ? It's very easy:
1- First you need to know what url you want to post to using curl
2- You need to know what are the names of variables to post (textarea, checkbox..)
3- Their number
4-And their values

This is a quite easy exemple:
if the original form has these fields: textarea1, textarea2, and sends to "www.site.com/page.php", like this example:
<form name="form1" method="post" action="www.site.com/page.php">
  <textarea name="textarea1"></textarea>
  <textarea name="textarea2"></textarea>
</form>

You should use this function in this way:
post_content("http://www.site.com/page.html",2,"textarea1=value1&textarea2=value2")

$nfields=2 because you have two fields (textarea1 and textarea2) ,
$fields_sring = "textarea1=value1&textarea2=value2" because you want to post two variables ( fields values) value1 for the field textarea1 and value2 for the field textarea2, you have to write always "&" between each couple of fieldname-value.

What does this all mean, and for what ?
With this function you will be able to "assimilate" the use of the form and the submit button, and this mean you will be able to get the content of the page www.site.com/pahe.php without using the form, because php can't use a form :)

That's all !

13 comments:

Julien DUBOIS said...

How does it happen if you want to post an image?
Thanks.

Diablo 3 Gold said...


excellent pics!!!! really country side result, adore the attire consequently elegant and wonderful!!! buy Diablo 3 gold



Cheap GW2 Gold

summerlijia said...

The legendary doomsday is around the corner, so who do you want to be together in your last minutes of life? Do not leave any regrets and enjoy yourself as much as possible. I plan to play diablo 3 games all day. Buy diablo 3 gold to do diablo 3 power leveling. Do you want to join me?

summerlijia said...

Do you believe that Dec. 21th 2012 is the end of the world? Wow, it's very horrific. What do you want to spent those days? Have you made the full preparation for the escaping. As long as to me, I do nothing but looking for cheap diablo 3 gold. I wonder if any store will cut the price or even sent free gold to customers before the doomsday. So if you are a diablo 3 fan too, hurry up to buy diablo 3 gold.

rscredits said...

It's remarkable to see your nice runescape gold introduction, and I'm going to bookmark rs gold this and come back to it. Keep writing! I really love your blog and benefit of your buy runescape gold easy decriptions.

Guild wars 2 gold said...

Again fantastic submit. Come on and blogging your website may well be more attractive. I always enjoy the interviews. I'm a editor of game site, you can visit and find diablo 3 gold, i like it very much.

china tours said...

Wow, wondeгful blog layout! Hoω long have you been blogging for?
you mаke blogging look eаsy.You can learn more: China tour packages | China travel packages | China Travel Agency

China tours said...

That is really cool. Thanks for finding it. Some really good ideas there, this is going to be a real time sink. I am a china tour lover,You can learn more: China vacation packages | China city tours | China Travel Agency

Learn Chinese said...

AllMandarin is the only company that can give you an excellent alternative to learn Mandarin Chinese that is not only better, but also more efficient in terms of cost, flexibility and learning methods.I am a Chinese teacher,you can learn more about Free Chinese language info: Learn mandarin online 1-on-1 | Learn mandarin Chinese online

Mary Jo Saul said...

Hello,

Can some one tell me how can I post some input to this page ??

http://myphpsource.blogspot.com/2009/12/php-post-data-to-url-with-curl.html

Full method plz..

Learn Chinese said...

The best place to learn Chinese online is in China. However, we understand that it isn't always possible to move here to study Chinese language. The next best thing is to study with our experienced teachers in a virtual classroom. Online students enjoy the same excellent way of Mandarin Online Course and custom designed courseware that we provide for our face to face clients.

Yatika Dhingra said...

Nice ..Thanx for sharing….
Supplier of Sandstone
Manufacturer of sandstone
Stone in India
Exporter of sandstone
Stone in india
Stone supplier
Agra red sandstone
Dholpur Beige sandstone
Katni Grey sandstone
Katni yellow Sandstone
Lalitpur Grey sandstone
Lalitpur Yellow Sandstone
Sagar Black sandstone
Fossil Mint Sandstone
Modak Pink Sandstone
Yellow Sandstone/stone
Mosaic Tiles in India
Sandstone Articles
Granites in India
Shree Ganesh stones
Kandla Grey Sandstone
Bijolia Sandstone
Kota green stone
Kota Brown
Stones in Rajasthan
Stones in Madhya Pradesh
Mint stone in india

Tanu Chauhan said...

You tried something new!! Good for you! I’m always afraid I’ll mess up when I try new stuff. What made you decide to try something new? I’d like to get motivated. Maybe you have some ideas you can share. Java training jalandhar