Why we need PHP CURL ?
To send HTTP GET requests, simply we can use file_get_contents() method.
But sending POST request and handling errors are not easy with file_get_contents().
Sending HTTP requests is very simple with PHP CURL.You need to follow the four steps to send request.
step 1). Initialize CURL session
step 2). Provide options for the CURL session
CURLOPT_URL -> URL to fetch
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.
step 3). Execute the CURL session
step 4). Close the session
Note: You can check whether CURL enabled/not with the following code.
To send HTTP GET requests, simply we can use file_get_contents() method.
file_get_contens('http://hayageek.com') |
Sending HTTP requests is very simple with PHP CURL.You need to follow the four steps to send request.
step 1). Initialize CURL session
1
| $ch = curl_init(); |
1
2
3
| curl_setopt($ch,CURLOPT_URL,"http://hayageek.com");curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers |
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.
step 3). Execute the CURL session
1
| $output=curl_exec($ch); |
curl_close($ch); |
$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);// curl_setopt($ch,CURLOPT_HEADER, false); $output=curl_exec($ch); curl_close($ch); return $output;
No comments:
Post a Comment