{"flag":true,"single":true,"pageTitle":"CURL in php","post":{"id":157,"user_id":"1","slug":"curl-in-php-vr9s","title":"CURL in php","body":"<p><strong>1. GET REQUEST<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>function api_get($url)\r\n{\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : ''));\r\n    if (strpos($url, 'https') === false) {\r\n        curl_setopt($ch, CURLOPT_PORT, 80);\r\n    } else {\r\n        curl_setopt($ch, CURLOPT_PORT, 443);\r\n    }    \r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); \/\/if https:\/\/www.google.com\/search?q=test want to redirect some other like google.in\/?etc\r\n    \/\/ Set the User-Agent string\r\n    $userAgent = \"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36\";\r\n    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);\r\n    $headers = [\r\n        \"Accept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8\",\r\n        \"Accept-Language: en-US,en;q=0.5\",\r\n        \"Connection: keep-alive\",\r\n        \"Upgrade-Insecure-Requests: 1\"\r\n    ];\r\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n\r\n    $tuData = curl_exec($ch);\r\n    \r\n    if (curl_errno($ch)) {\r\n        print_r(curl_error($tuCurl));\r\n    }\r\n    curl_close ($ch);\r\n    return $tuData;\r\n}\r\necho api_get(\"https:\/\/www.google.com\/\");<\/code><\/pre>\r\n<p>2.&nbsp;<br>#----------------------------------------#<br>How to use simplehtmldom using Curl<br>Open a url and find all anchors&nbsp;<br>#----------------------------------------#<\/p>\r\n<pre class=\"language-markup\"><code>\/\/ download from here https:\/\/sourceforge.net\/projects\/simplehtmldom\/\r\ninclude 'simple_html_dom.php';\r\n$result = api_get(\"https:\/\/www.google.com\/search?q=test\");\r\n$html = new simple_html_dom();\r\n$html-&gt;load($result);\r\nforeach($html-&gt;find('a') as $link){\r\n    echo $link-&gt;plaintext.\"&lt;br\/&gt; &lt;b&gt; Link: &lt;\/b&gt;\".$link-&gt;href.\"&lt;br\/&gt;&lt;br\/&gt;\";<\/code><\/pre>\r\n<p>3.&nbsp; Post Request using curl<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nfunction post_req($url, $parameters) {\r\n    \/\/ Initialize cURL session\r\n    $ch = curl_init($url);\r\n    $postFields = http_build_query($parameters);\r\n    curl_setopt($ch, CURLOPT_POST, true); \/\/ Set HTTP method to POST\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); \/\/ Set POST parameters\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); \/\/ Return response as a string instead of outputting directly\r\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); \/\/ Disable SSL certificate verification (not recommended for production)\r\n    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); \/\/ Disable SSL host verification (not recommended for production)\r\n    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); \/\/ Follow any \"Location:\" header that the server sends\r\n    curl_setopt($ch, CURLOPT_HEADER, true); \/\/ Include header in the output\r\n    $response = curl_exec($ch);\r\n\r\n    if (curl_errno($ch)) {\r\n        echo 'cURL error: ' . curl_error($ch);\r\n    }\r\n\r\n    \/*$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);\r\n    $header = substr($response, 0, $header_size);\r\n    $body = substr($response, $header_size);*\/\r\n\r\n    curl_close($ch);\r\n    return $body;\r\n}\r\n\r\n$url = \"https:\/\/leadmcqs.com\/bin\/\";\r\n$parameters = array(\r\n    'text' =&gt; 'geo Curl',\r\n    'param2' =&gt; 'value2',\r\n    \/\/ Add more parameters as needed\r\n);\r\n\r\n$response = post_req($url, $parameters);\r\necho $response;\r\n<\/code><\/pre>\r\n<p>4. Login to website and access other pages<\/p>\r\n<pre class=\"language-markup\"><code>$postFields=array(\r\n    \"log\"=&gt;\"zaryab\",\r\n    \"pwd\"=&gt;\"9LIQZ^8lT6BqGLvYsc\",\r\n    \"rememberme\"=&gt;1,\r\n    \"wp-submit\"\r\n    );\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL,\"https:\/\/abc.com\/wp-login.php\");\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($ch, CURLOPT_POST,1);\r\n    \/\/curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); \/\/if web is secure\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postFields));\r\n    curl_setopt($ch, CURLOPT_COOKIEJAR, \"cookies.txt\");\r\n    curl_setopt($ch, CURLOPT_COOKIEFILE, \"cookies.txt\");\r\n\r\n    $result = curl_exec($ch);\r\n        if(curl_errno($ch)){\r\n            echo 'Curl error: '.curl_error($ch);\r\n        }\r\n    \/\/login succes now we access other pages\r\n    for($i=1;$i&lt;=2;$i++){\r\n        curl_setopt($ch, CURLOPT_URL,\"https:\/\/abc.com\/wp-admin\/edit.php?post_type=page\");\r\n        curl_setopt($ch, CURLOPT_POST,1);\r\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n        curl_setopt($ch, CURLOPT_COOKIEJAR, \"cookie.txt\");\r\n        $result2 = curl_exec($ch);\r\n        echo $result2;\r\n        }\r\n        curl_close($ch);<\/code><\/pre>","category_id":"1","is_private":"0","created_at":"2023-06-06T01:53:25.000000Z","updated_at":"2024-06-18T08:31:45.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"1. GET REQUEST function api_get($url) {     $ch = curl_init();     curl_setopt($ch, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : - CURL in php (Updated: June 18, 2024) - Read more about CURL in php at my programming site [SITE]","categories":[]}