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 !

HTML code directly in IF Conditions

This is the most amazing thing in php conditions, and exactly the if statement.
In a php condition, you can put directly a html code without the echo function :)

This is what I'm talking about, with echo function, then without.:



if ($condition == 'something') {
echo '
My Website...
'; } else { echo '
My Website...
'; } ?>
With the new way I'm talking about, the script will be like this:




 if ($condition = 'something') {?>
<div class="style1">My Website...<div>
 } else { ?>
<div class="style2">My Website2...<div>
 }?>




Now  why this can be useful ?
In fact, when you are using a html editor like dreamweaver, you will be able to design easily the html code in the if statement, you can add tables, images, div, change colors ... directly using the design view, or when you use the code view you can see the html code highlighted (like in the exemple above) wich believe me will help you a lot and make your programming so much easier !


Thank you.

PHP forms

Using PHP With HTML Forms

We use forms to interact with the website visitor, we can get and save his name, email address, comments, and even his purchased item, color of the item, his street address.
The form is made in two steps: creating a html form to be visible in the browser of the visitor, and creating a php script that will process the form, such as sending email, saving name and email in database, ...

The complete Tutorial is here: PHP Forms

Tutorials-Planet.com

I'm moving to Tutorials-planet.com, you can find there all php articles, tutorials and script you need to be a successfull php programmer.
All php sources should be here
PHP tutorials include strings, variables, functions, arrays and form tutorials, other tutorials are coming !