What is cURL and how to POST Form Data and file with cURL with Example

What is cURL and how to POST Form Data and file with cURL with Example

Before going to know how to post form data and file with cURL, i will tell you, what is cURL in PHP and how will you use cURL in PHP script.

cURL is a library and command line tool which is used to get and send file using URL syntax and become powerful system.

cURL was released in 1997 and it was known as "See URL" when it was released.

cURL supports various protocols including HTTP, HTTPS, FTP, FTPS, TFTP, SCP, SFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP and RTSP.

Mostly we use cURL for accessing third-party API system like : sending sms via API using cURL.

Now a days developers call it "curl in PHP".

Steps to use cURL
  • First i initialize curl session by using curl_init method.
  • Second i set different option for curl session by using curl_setopt method and some are optional.
  • Third i execute curl session to get and send data over server by using curl_exec.
  • Fourth i close the curl session by using curl_close which i have created.
  • Now you can display response data.

Here you will fine basic cURL example.

  1. function getContent($url, $postdata = false)
  2. {
  3. // check if cURL installed or not in your system?
  4. if (!function_exists('curl_init')){
  5. return 'Sorry cURL is not installed!';
  6. }
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  11. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  12. if ($postdata)
  13. {
  14. curl_setopt($ch, CURLOPT_POST, 1);
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  16. }
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  18. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36");
  19. $contents = curl_exec($ch);
  20. $headers = curl_getinfo($ch);
  21. curl_close($ch);
  22. return array($contents, $headers);
  23. }

You can send post data to define option CURLOPT_POST and data will be passed with CURLOPT_POSTFIELDS option.

Now i will tell you some cURL command to posting form data.

Use curl -X POST and add -F for every parameters which you want to add to the POST.

  1. curl -X POST -F 'field1=value1' -F 'field2=value2' http://your-domain.com/page.php

Now on server you can receive these data by using $_POST and if you want to see data to make sure then you can print $_POST variable by using print_r method.

Output which you will get :
  1. Array(
  2. 'field1' => 'value1',
  3. 'field2' => 'value2'
  4. )

You can send header or specific data type with cURL then use -H to add header.

  1. curl -X POST -H 'Content-Type: application/json' -d '{"field1":"davidwalsh","field2":"something"}' http://your-domain.com/page.php

-d is using to send raw data.

You can send files with cURL but make sure while sending files with cURL you will have to add @ before the file path.

  1. curl -X POST -F 'image=@/image-path/mypicture.jpg' http://your-domain.com/upload-image

You can access files on server by using $_FILES variable.

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort