Blogging has been a great platform to share useful stuffs. Like tips, secrets, marketing and so on. But today let’s have knowledge about some technical stuff cURL. Have you ever heard about it? No? Carry on then.
cURL is a very useful function which helps to transfer data between the servers. The curl and its library act as a bridge between your server and the server whom you want to communicate. You can easily enable curl function into your Wamp server and use its functionality. Let’s know about how to enable cURL?
Steps To Enable cURL Function
- Go to Wamp Server –> PHP –> Open php.ini file
- Find php_curl.dll statement in the php.ini file.
- Uncomment the statement by removing the semicolon and save the file.
- Create a new file let say info.php you can give any name to a file you want and write respective statements and save the file into your Wamp server.
Check whether the cURL is now enabled or not
Run info.php file and you can easily check whether the cURL is enable or not. As shown in the image given below.
Steps to use cURL function
1. Fetch webpage data
You can set connection with any website by giving the URL address as I mention my https://www.inkthemes.com/ website in the image given below and fetch data from the site.
Create curl.php file or you can give any name to a file and write the code given below.
Now run the curl.php file. You can see the webpage of the website.
2. Store webpage data into a file
If you want to store the data in any of the file you want. You can see here the somefile.php is the file generated dynamically which stores the data of https://socialtriggers.com/. You can give any name to a file or you can create it also. You can see in the image given below.
Now run the above code stored in curl.php file and then somefile.php file. As you can see in the image given below the somefile stores the data of https://socialtriggers.com/ webpage.
3. Fetch file data
Create a file let say posttest.php which stores some data shown in the image given below.
You can fetch the data of posttest.php file directly by using the coding given below. Let say it stored in curl.php file
Now execute the curl.php file you get the output given below.
When you set the boolean value false as in the above code then the output gets changed. You can view in the image given below.
The output come like this.
This is because when you set the boolean value false –> CURLOPT_RETURNTRANSFER print the result and then curl_exec( ) executes which gives boolean value. As a result $data assign with a boolean value “1”.
In case if you set boolean value true –> the control moves to curl_exec( ) and first execute this function as a result $data now holds the return value that comes from posttest.php file and overrides the boolean value.
4. Set time to establish connection
You can set the connection time which allows curl to establish connection with the web page within that limit. If the connection does not establish within the set time limit then the sorry message appears on the output screen or the message you want to print. You can use the code given below.
If the connection does not establish within the desired time limit then the message appears on the screen like this.
Conclusion
I hope you will find this tutorial useful. You can easily follow the above steps in order to enable and use cURL function in your program.
It is not complicated at all. If you need any help let me know I will definitely resolve your queries.