Thursday, 30 December 2021

PHP CURL Basic Usage

 https://stackoverflow.com/questions/8115683/php-curl-custom-headers

// Set headers
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Cache-Control: no-cache',
    'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
    'Host: www.example.com',
    'Referer: http://www.example.com/index.php', //Your referrer address
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
    'X-MicrosoftAjax: Delta=true'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;


// Curl defaults including curl_setopt_array
$Defaults = array(
//		CURLOPT_HEADER				=>	TRUE,
		CURLOPT_RETURNTRANSFER      =>  TRUE,
		CURLOPT_FAILONERROR         =>  TRUE,
		CURLOPT_CONNECTTIMEOUT      =>  0,
		CURLOPT_TIMEOUT             =>  5,
		CURLOPT_ENCODING            =>  "",
		CURLOPT_MAXREDIRS           =>  10,
		CURLOPT_HTTP_VERSION        =>  CURL_HTTP_VERSION_1_1,
		CURLOPT_SSL_VERIFYPEER		=>	FALSE,
		CURLOPT_SSL_VERIFYHOST		=>	FALSE,
	);
		$curl = curl_init();
		curl_setopt_array( $curl, $Defaults );

// https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl
/ / curl with JSON
                curl_setopt_array( $curl, array(
                    CURLOPT_CUSTOMREQUEST		=>	'POST',
                    CURLOPT_POSTFIELDS			=>	json_encode( $data),
                    CURLOPT_URL					=> $url
                    CURLOPT_HTTPHEADER          =>  array(
                                                        'Content-Type: application/json',
                                                        'Accept: application/json',
                                                        'Authorization: Bearer ' .  $token,
                                                    ),
                ) );

No comments:

Post a Comment