Making multiple requests to the same site with similar options can be a pain, since you end up repeating yourself. The Session object can be used to set default parameters for these.
Let’s simulate communicating with GitHub.
$session = new Requests_Session('https://api.github.com/'); $session->headers['X-ContactAuthor'] = 'rmccue'; $session->useragent = 'My-Awesome-App'; $response = $session->get('/zen');
You can use the
options properties of the Session
object to set the defaults for this session, and the constructor also takes
parameters in the same order as
Requests::request(). Accessing any other
properties will set the corresponding key in the options array; that is:
// Setting the property... $session->useragent = 'My-Awesome-App'; // ...is the same as setting the option $session->options['useragent'] = 'My-Awesome-App';
Secure Requests with SSL
By default, HTTPS requests will use the most secure options available:
$response = Requests::get('https://httpbin.org/');
Requests bundles certificates from the Mozilla certificate authority list, which is the same list of root certificates used in most browsers. If you’re accessing sites with certificates from other CAs, or self-signed certificates, you can point Requests to a custom CA list in PEM form (the same format accepted by cURL and OpenSSL):
$options = array( 'verify' => '/path/to/cacert.pem' ); $response = Requests::get('https://httpbin.org/', array(), $options);
Alternatively, if you want to disable verification completely, this is possible
'verify' => false, but note that this is extremely insecure and should be
Requests supports SSL across both cURL and fsockopen in a transparent manner.
Unlike other PHP HTTP libraries, support for verifying the certificate name is
built-in; that is, a request for
https://github.com/ will actually verify the
certificate’s name even with the fsockopen transport. Requests was the
first PHP HTTP library to fully support SSL verification.
Previous: Making a request