Mengirimkan request menggunakan cURL pada PHP

 Softberg Blog | cURL techniques in Quantum - Softberg Blog

 

Mengirimkan sebuah request pada cURL dengan method GET

<?php  
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https:/www.google.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $respone;
curl_close($ch)
?>

 

Mendapatkan HTTP code dari request

<?php
$url = 'http://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo 'HTTP code: ' . $httpcode;
?>

 

Mengirimkan sebuah request cURL dengan method POST

<?php
$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];

$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$response = curl_exec($ch);

curl_close($ch);

echo $response;
?>

 

Bypass HTTP response 301 and 302

<?php
$url = 'example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo $output;
echo '<script>alert("HTTP Response: ' . $httpcode.'")</script>';
?>


Mengirimkan banyak request sekaligus menggunakan curl_multi_init() dan mendapatkan response dari request2 tersebut dan mernampilkannya semua
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
$ch3 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch3, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch3, CURLOPT_HEADER, 0);
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
curl_multi_add_handle($mh,$ch3);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_remove_handle($mh, $ch3);
curl_multi_close($mh);

 
// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch1);
$response_2 = curl_multi_getcontent($ch2);
$response_3 = curl_multi_getcontent($ch3);
echo $response_1,'<br><br><br>' ,$response_2,'<br><br><br>' ,$response_3; // output results
?>

Komentar