-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
Post a Comment