基础代码
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Now let's make a request!
|
||||
$options = array(
|
||||
'auth' => array('someuser', 'password')
|
||||
);
|
||||
$request = Requests::get('http://httpbin.org/basic-auth/someuser/password', array(), $options);
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Say you need to fake a login cookie
|
||||
$c = new Requests_Cookie('login_uid', 'something');
|
||||
|
||||
// Now let's make a request!
|
||||
$request = Requests::get('http://httpbin.org/cookies', array('Cookie' => $c->formatForHeader()));
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Say you need to fake a login cookie
|
||||
$c = new Requests_Cookie_Jar(['login_uid' => 'something']);
|
||||
|
||||
// Now let's make a request!
|
||||
$request = Requests::get(
|
||||
'http://httpbin.org/cookies', // Url
|
||||
[], // No need to set the headers the Jar does this for us
|
||||
['cookies' => $c] // Pass in the Jar as an option
|
||||
);
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Now let's make a request!
|
||||
$request = Requests::get('http://httpbin.org/get', array('Accept' => 'application/json'));
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Setup what we want to request
|
||||
$requests = array(
|
||||
array(
|
||||
'url' => 'http://httpbin.org/get',
|
||||
'headers' => array('Accept' => 'application/javascript'),
|
||||
),
|
||||
'post' => array(
|
||||
'url' => 'http://httpbin.org/post',
|
||||
'data' => array('mydata' => 'something'),
|
||||
),
|
||||
'delayed' => array(
|
||||
'url' => 'http://httpbin.org/delay/10',
|
||||
'options' => array(
|
||||
'timeout' => 20,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Setup a callback
|
||||
function my_callback(&$request, $id) {
|
||||
var_dump($id, $request);
|
||||
}
|
||||
|
||||
// Tell Requests to use the callback
|
||||
$options = array(
|
||||
'complete' => 'my_callback',
|
||||
);
|
||||
|
||||
// Send the request!
|
||||
$responses = Requests::request_multiple($requests, $options);
|
||||
|
||||
// Note: the response from the above call will be an associative array matching
|
||||
// $requests with the response data, however we've already handled it in
|
||||
// my_callback() anyway!
|
||||
//
|
||||
// If you don't believe me, uncomment this:
|
||||
# var_dump($responses);
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Now let's make a request!
|
||||
$request = Requests::post('http://httpbin.org/post', array(), array('mydata' => 'something'));
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Now let's make a request via a proxy.
|
||||
$options = array(
|
||||
'proxy' => '127.0.0.1:8080', // syntax: host:port, eg 12.13.14.14:8080 or someproxy.com:3128
|
||||
// If you need to authenticate, use the following syntax:
|
||||
// 'proxy' => array( '127.0.0.1:8080', 'username', 'password' ),
|
||||
);
|
||||
$request = Requests::get('http://httpbin.org/ip', array(), $options );
|
||||
|
||||
// See result
|
||||
var_dump($request->body);
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Set up our session
|
||||
$session = new Requests_Session('http://httpbin.org/');
|
||||
$session->headers['Accept'] = 'application/json';
|
||||
$session->useragent = 'Awesomesauce';
|
||||
|
||||
// Now let's make a request!
|
||||
$request = $session->get('/get');
|
||||
|
||||
// Check what we received
|
||||
var_dump($request);
|
||||
|
||||
// Let's check our user agent!
|
||||
$request = $session->get('/user-agent');
|
||||
|
||||
// And check again
|
||||
var_dump($request);
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
// First, include Requests
|
||||
include('../library/Requests.php');
|
||||
|
||||
// Next, make sure Requests can load internal classes
|
||||
Requests::register_autoloader();
|
||||
|
||||
// Define a timeout of 2.5 seconds
|
||||
$options = array(
|
||||
'timeout' => 2.5,
|
||||
);
|
||||
|
||||
// Now let's make a request to a page that will delay its response by 3 seconds
|
||||
$request = Requests::get('http://httpbin.org/delay/3', array(), $options);
|
||||
|
||||
// An exception will be thrown, stating a timeout of the request !
|
||||
Reference in New Issue
Block a user