| home / programming / php / cookbook /chap11 / 1 | [previous] [next] |
|
|
You want to retrieve a page that requires a cookie to be sent with the request for the page.
Use the cURL extension and the CURLOPT_COOKIE
option:
$c = curl_init('http://www.example.com/needs-cookies.php');curl_setopt($c, CURLOPT_VERBOSE, 1);curl_setopt($c, CURLOPT_COOKIE, 'user=ellen; activity=swimming');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);$page = curl_exec($c);curl_close($c);
If cURL isn't available, use the addHeader(
) method in the PEAR HTTP_Request class:
require 'HTTP/Request.php';$r = new HTTP_Request('http://www.example.com/needs-cookies.php');$r->addHeader('Cookie','user=ellen; activity=swimming');$r->sendRequest();$page = $r->getResponseBody();
Cookies are sent to the server in the Cookie
request header. The cURL extension has a cookie-specific option, but with HTTP_Request,
you have to add the Cookie header just as with
other request headers. Multiple cookie values are sent in a semicolon-delimited
list. The examples in the Solution send two cookies: one named user
with value ellen and one named activity
with value swimming.
To request a page that sets cookies and then make subsequent requests
that include those newly set cookies, use cURL's "cookie jar" feature.
On the first request, set CURLOPT_COOKIEJAR to
the name of a file to store the cookies in. On subsequent requests, set CURLOPT_COOKIEFILE
to the same filename, and cURL reads the cookies from the file and sends them
along with the request. This is especially useful for a sequence of requests
in which the first request logs into a site that sets session or authentication
cookies, and then the rest of the requests need to include those cookies to
be valid:
$cookie_jar = tempnam('/tmp','cookie');// log in$c = curl_init('https://bank.example.com/login.php?user=donald&password=b1gmoney$');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);$page = curl_exec($c);curl_close($c);// retrieve account balance$c = curl_init('http://bank.example.com/balance.php?account=checking');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);$page = curl_exec($c);curl_close($c);// make a deposit$c = curl_init('http://bank.example.com/deposit.php');curl_setopt($c, CURLOPT_POST, 1);curl_setopt($c, CURLOPT_POSTFIELDS, 'account=checking&amount=122.44');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);$page = curl_exec($c);curl_close($c);// remove the cookie jarunlink($cookie_jar) or die("Can't unlink $cookie_jar");
Be careful where you store the cookie jar. It needs to be in a place your web server has write access to, but if other users can read the file, they may be able to poach the authentication credentials stored in the cookies.
Documentation on curl_setopt( ) at
http://www.php.net/curl-setopt; the PEAR HTTP_Request
class at http://pear.php.net/package-info.php?package=HTTP_Reques
You want to retrieve a URL that requires specific headers to be sent with the request for the page.
Use the cURL extension and the CURLOPT_HTTPHEADER
option:
$c = curl_init('http://www.example.com/special-header.php');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_HTTPHEADER, array('X-Factor: 12', 'My-Header: Bob'));$page = curl_exec($c);curl_close($c);
If cURL isn't available, use the addHeader(
) method in HTTP_Request:
require 'HTTP/Request.php';$r = new HTTP_Request('http://www.example.com/special-header.php');$r->addHeader('X-Factor',12);$r->addHeader('My-Header','Bob');$r->sendRequest();$page = $r->getResponseBody();
cURL has special options for setting the Referer
and User-Agent request headers-- CURLOPT_REFERER
and CURLOPT_USERAGENT:
$c = curl_init('http://www.example.com/submit.php');curl_setopt($c, CURLOPT_VERBOSE, 1);curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_REFERER, 'http://www.example.com/form.php');curl_setopt($c, CURLOPT_USERAGENT, 'CURL via PHP');$page = curl_exec($c);curl_close($c);
Parsing a Web Server Log File explains why "referrer"
is often misspelled "referer" in web programming contexts; documentation
on curl_setopt( ) at http://www.php.net/curl-setopt;
the PEAR HTTP_Request class at http://pear.php.net/package-info.php?package=HTTP_Request.
You want to retrieve a secure URL.
Use the cURL extension with an HTTPS URL:
$c = curl_init('https://secure.example.com/accountbalance.php');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);$page = curl_exec($c);curl_close($c);
To retrieve secure URLs, the cURL extension needs access to an SSL library, such as OpenSSL. This library must be available when PHP and the cURL extension are built. Aside from this additional library requirement, cURL treats secure URLs just like regular ones. You can provide the same cURL options to secure requests, such as changing the request method or adding POST data.
The OpenSSL Project at http://www.openssl.org/.
| home / programming / php / cookbook /chap11 / 1 | [previous] [next] |
Created: March 27, 2003
Revised: March 27, 2003
URL: http://webreference.com/programming/php/chap11/1