drewgg
2/13/2017 - 2:02 PM

PHP Curl calls

PHP Curl calls

// -----------------------------------------------------------------------------
// Basic curl to retreve the contents of a page

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$reply = curl_exec($ch);
curl_close($ch);

// -----------------------------------------------------------------------------
// Curl with GET vars



// -----------------------------------------------------------------------------
// Curl with POST vars

		$postData =
		[
			'first_name' => $firstName,
			'last_name' => $lastName,
			'email' => $email,
			'id' => $ip,
		];

		$payload = http_build_query($postData);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $requestUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		$reply = curl_exec($ch);
		curl_close($ch);

// Curl with HTTPS

		$headers =
		[
			'Content-Length: ' . strlen($payload),
		];
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->requestUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_CAINFO, '/home/stramail/cacert.pem');
		$reply = curl_exec($ch);
		curl_close($ch);