Simple Client URL example in PHP

Here’s a simple example of using the cURL program through PHP’s client URL library. It’s a really powerful way to use HTTP, because you can set just about every HTTP option using the library.

<?php
	// if you need a password for the site:
	$user = "user";
	$password = "password";

	// the URL you want to reach:
	$url = 'http://myserver.com/';

	// initialize the URL call parameters:
	$request = curl_init();
	// set the URL to reach:
	curl_setopt($request, CURLOPT_URL, $url);
	// add this line back in if you need a password:
 	// curl_setopt($request, CURLOPT_USERPWD, "$user:$password");

 	// ask for the result as a string;
	curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

	// get the results:
	$response = curl_exec($request);
	// print out the results:
	echo $response;
?>
This entry was posted in PHP. Bookmark the permalink.

Comments are closed.