Developer Snippet Diary

CURL in php

1. GET REQUEST

function api_get($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : ''));
    if (strpos($url, 'https') === false) {
        curl_setopt($ch, CURLOPT_PORT, 80);
    } else {
        curl_setopt($ch, CURLOPT_PORT, 443);
    }    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //if https://www.google.com/search?q=test want to redirect some other like google.in/?etc
    // Set the User-Agent string
    $userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    $headers = [
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language: en-US,en;q=0.5",
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1"
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $tuData = curl_exec($ch);
    
    if (curl_errno($ch)) {
        print_r(curl_error($tuCurl));
    }
    curl_close ($ch);
    return $tuData;
}
echo api_get("https://www.google.com/");

2. 
#----------------------------------------#
How to use simplehtmldom using Curl
Open a url and find all anchors 
#----------------------------------------#

// download from here https://sourceforge.net/projects/simplehtmldom/
include 'simple_html_dom.php';
$result = api_get("https://www.google.com/search?q=test");
$html = new simple_html_dom();
$html->load($result);
foreach($html->find('a') as $link){
    echo $link->plaintext."<br/> <b> Link: </b>".$link->href."<br/><br/>";

3.  Post Request using curl

<?php
function post_req($url, $parameters) {
    // Initialize cURL session
    $ch = curl_init($url);
    $postFields = http_build_query($parameters);
    curl_setopt($ch, CURLOPT_POST, true); // Set HTTP method to POST
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); // Set POST parameters
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string instead of outputting directly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL certificate verification (not recommended for production)
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disable SSL host verification (not recommended for production)
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); // Follow any "Location:" header that the server sends
    curl_setopt($ch, CURLOPT_HEADER, true); // Include header in the output
    $response = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    }

    /*$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);*/

    curl_close($ch);
    return $body;
}

$url = "https://leadmcqs.com/bin/";
$parameters = array(
    'text' => 'geo Curl',
    'param2' => 'value2',
    // Add more parameters as needed
);

$response = post_req($url, $parameters);
echo $response;

4. Login to website and access other pages

$postFields=array(
    "log"=>"zaryab",
    "pwd"=>"9LIQZ^8lT6BqGLvYsc",
    "rememberme"=>1,
    "wp-submit"
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://abc.com/wp-login.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST,1);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //if web is secure
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postFields));
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");

    $result = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Curl error: '.curl_error($ch);
        }
    //login succes now we access other pages
    for($i=1;$i<=2;$i++){
        curl_setopt($ch, CURLOPT_URL,"https://abc.com/wp-admin/edit.php?post_type=page");
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
        $result2 = curl_exec($ch);
        echo $result2;
        }
        curl_close($ch);
Posted by: R GONDAL
Email: [email protected]