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 !

Free web hosting services

Every one who want to start online business, need a very cheap or free web hosting to start with.

Many companies online provide a good free web hosting services, and they may differ by their:
  • Disk Space
  • Data Transfer / bandwidth
  • databases
  • email services
  • usable languages: php/asp/perl ...
All are free, but only the file hosting is free, and surely not the domain name. In every web hosting plan, you need to buy your own domain name ( whatever from your hosting company or another company ), or, for your quick start and testing, you can use their free subdomain like: yourname.theirdomain.com or myphpsource.blogspot.com where blogger (blogspot) is the hosting company who offer free subdomain.

I collected for you the best free web hosting services, with their reliable/dedicate server. Where you can apply for a free subdomain along with the hosting, or attach your own domain name to their servers.


-> orgfree.com
they offer good webhosting, with ftp, web-based file manager, php, mysql database, but they add their own ads to all your pages, which not seems to be good for many people. see orgfree.com.

-> zymic.com
This is an excellent free web hosting service with 6000 MB hosting space, php, 5 MySQL databases, ftp & file manager ...
They are also ad-free hosting so they do not add their ads to your pages, and they also provide you with quality free templates for your new website. See their details here.

-> Gofreeserve.com
Gofreeserve.com is my best at all ! 99.9% server uptime, it's a reliable webhosting service: 1000 MB hosting space, 15 Add-on domains, 15 Sub domains, Website Builder, Webmail (RoundCube), 15 MySQL databases, php MyAdmin, no ads and much more in their free hosting plan that you can see here.

-> 50gigs.net
50gigs.net is also a good webhosting, 5000 MB hosting space, 50 Add-on domains, 50 Sub domains, Website Builder, free RoundCube Webmail, MySQL databases, no ads. You can see more here.

I will post more and more good and free webhosting services, or cheap hosting, or reliable.

Connect to Mysql Database

Connecting to the mysql database is done in two steps:
  1. Connecting to a mysql server, it's the most of time installed in the same server where php is, so we uselocalhost as address of mysql server.

    mysql_connect("localhost", "username", "password") or die(mysql_error());
    echo "Connected to MySQL Server";

    Some times, when you are running a server in your own computer and not a hosting service provider's, you can connect with just indicating username "root" with no password, and some times is no need to enter an username. It's all depend on your current mysql configuration.

  2. After you are connected to a mysql server, you need then to connect to a mysql database:

    mysql_select_db("database-name") or die(mysql_error());
    echo "Connected to Mysql Database";

So now you are completely connected to your database, and you can read/send data from/to it. But if a problem occurs, the "or die(mysql_error());" help you to find the error and hundle it.

About Mysql Database

So you want to know what is mysql ? Or already now it and want to learn how to use it with php ? In all cases we are here to help you !

Mysql is useful when you want to store data, infos... Yes you can save your data in a text file, but will not be easy to read details, to search, or any other task...

For example, let's say you want to store details of users of your website, every user will have an username, password, and email , you can so create a database, with a table "users", and that table has 3 columns: username; password; email, and each user occupies one line. It's exactly like Excel documents...
In that example, you can store data in a text file, each user per line, his details seperated with ";" , but that will make a big file if you have many users, so script will be slow in reading and writing, and the most important thing: searching.
Mysql is known as a fast database, and very smart one.

You can use many tables in the same mysql database, and with relations between them, which make it the best database.

Mysql is used with php, but has its own language, so php functions will send mysql scripts to mysql server, then the server do the tasks, and send back results to php where you can manipulate them.
You can for example read all details from a mysql table, or insert a new entry, update one, or delete another, or search in the table and get the matching entries, or sort data, or create a new table ...

We will see step by step how to do all that,
Will see you again, bye !

Do you need a php programmer ?

Find programmers and grapic design experts at ScriptLance.com

The best solution to hire a programmer for a project you have, is to look for a freelancer.
But where ?

Ok, I know, there are many freelance websites and you are confused, or maybe you don't know any one, in all cases I advice you to take a look at scriptlance .

Yes, I like it, and I'm already a programmer there, you can register and post your project for free, so nothing to lose to take a look at :)

Why ?
There are many excellent programmers there, and they can do their job very quickly and very cheap !

It's also secure, as the money will be keept in an escrow account, and the programmer can't take it unless he complete his job and release the money to him. You can also pay with any method you like .

So, just go to scriptlance and, you will see a message in the home page: "Describe what you need and submit a project free and quickly"
Post there your project !

If you don't have a project yet, register for free, and post whenever you need.

. . . . . . . . . . . . . . . . . . . . . . .

Oups, I forget, if you are a programmer and want to become freelancer and get paid, just register as programmer, it's also free ..


Outsource your projects to thousands of programmers at ScriptLance.com

php Loop statements

Loops are used in programming to do the same tasks many times, while a condition is true...
That means, same script will be executed and repeated, until that condition becomes false.

The loop statements used in php are:
while
do ... while
for
foreach

while and do ... while are similar, the only difference is that, the first one will execute the script if and as long as the condition is true, but the second one, will execute the script, then repeat it as long as the condition is true. In brief, when condition is false while will not do the tasks and exit, but do ... while will do them and exit.

For is to repeat tasks a specific number of times, like you want to execute a script 10 times ....

Foreach to repeat tasks for each element in an array.

Next posts will show how to use these statements.

If you have any questions, post them.