Curl is the best way to get content of another website and traverse it according to your need. I already coverred PHP CURL tutorial with Examples in my previous posts. In this tutorial I will show you how to pass parameter in php curl get request
If you know wordpress a little then you notice that if you search anything on wordpress blog then it append values with blogurl/?s=value and display you search result. Just like below
Above image has query string in url and it also shows searched value in right bar input text.
We will be going to achieve exactly the same using curl.
URL:
1 2 3 |
$url = "https://www.wdb24.com/"; |
Query String:
1 2 3 |
$dataArray = array("s"=>'PHP CURL'); |
PHP curl get request:
Below php code search “PHP CURL” in wdb24.com. http_build_query() function encode given string.
If you want to learn more about htpp_build_query function then visit my post 10 PHP Url Functions – Every beginner must check
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ch = curl_init(); $data = http_build_query($dataArray); $getUrl = $url."?".$data; curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $getUrl); curl_setopt($ch, CURLOPT_TIMEOUT, 80); $response = curl_exec($ch); if(curl_error($ch)){ echo 'Request Error:' . curl_error($ch); } else { echo $response; } curl_close($ch); |
If you run above code to the browser then you will get output like below.
Also read:
- PHP MYSQLi Object Oriented Tutorial for Beginners
- How to pass $_GET to command line arguments in PHP (with example)
- PHP get random User Agent Function
- User Registration in PHP
- Check and Uncheck all Checkbox using jQuery