Skip to main content

What is cURL(Client URL) in Php?




-Whenever we make a HTTP request,we can make use of http methods like GET,POST,PUT..etc.
-If we want to use REST API's,we need a client which has the capability to use all HTTP methods.Here,HTTP is limited in this case.
-So,we need a client library,that's were CURL comes into play.We use CURL while consuming REST API's.

-Consuming a REST API using Php
1.curl_init() : Establishes a connection

2.curl_setopt($ch,int $option,mixed $value) : Add/Make a request
2.1.curl handled by curl_init().
2.2.CURLOPT_XXX option to set.Example : CURLOPT_URL.
2.3.$value is the value for option.Example : https://www.google.co.in for CURLOPT_URL.

3.curl_exec() :Executes the request by taking the curl handled by curl_init().
4.curl_close() :Closes the connection.

Example:

$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"https://www.google.co.in");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response=curl_exec($ch);
curl_close($ch);
print $result; //returns the homepage of google

Comments

Popular posts from this blog

Send Email(with subject,body,attachment) using SSMTP through Terminal in linux?

1.Install ssmtp as              $ sudo apt-get update && sudo apt-get install ssmtp 2.Configure smtp file by editing the following file as, $ sudo gedit /etc/ssmtp/ssmtp.conf Update file with the following: AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL FromLineOverride=YES mailhub=smtp.gmail.com:587 UseSTARTTLS=YES Now ssmtp.conf file should look like as the following one, AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL # # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587 # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=tele-desktop118 # A...

Backup (or) Download MYSQL Database as a zip file Using PHP

NOTE:Please change below credentials  with respect to your Database and Run the file. <?php define ( "DB_USER" ,  'DatabaseUSERNAME' ); define ( "DB_PASSWORD" ,  'DatabasePASSWORD' ); define ( "DB_NAME" ,  'DatabaseNAME' ); define ( "DB_HOST" ,  'localhost' ); define ( "BACKUP_DIR" ,  'myphp-backup-files' );  // Comment this line to use same script's directory ('.') define ( "TABLES" ,  '*' );  // Full backup //define("TABLES", 'table1, table2, table3'); // Partial backup of required tables define ( "CHARSET" ,  'utf8' ); define ( "GZIP_BACKUP_FILE" ,  true );   // Set to false if you want plain SQL backup files (not gzipped) class  Backup_Database {     var  $host ;     var  $username ;   ...

How to Integrate SMS API's with PHP?

<?php $message  =  "Happy Coding..A Test Message to Check API" ; $students  =  "9999999999"  //your mobile number to test $msg  =  urlencode ( $message );          $url = "http://www.bulksmsapps.com/api/apibulkv2.aspx?apikey=XXX-XXXX-XXXX-XXX-XXXXXXXX&sender=XXXXX&number=" . $students . "&message=" . $msg ; $curl_handle = curl_init (); curl_setopt ( $curl_handle , CURLOPT_URL , $url ); curl_setopt ( $curl_handle , CURLOPT_RETURNTRANSFER , 1 );  curl_setopt ( $curl_handle ,  CURLOPT_FOLLOWLOCATION ,  false ); set_time_limit ( 0 ); $response  =  curl_exec ( $curl_handle ); err  =  curl_error ( $curl_handle ); curl_close ( $curl_handle ); if ( $err ) {     echo  "cURL Error #:"  .  $err ; } else {     echo  $response ; } ?> ...