Curl::request PHP Method

request() public static method

[request 执行一次curl请求]
public static request ( [string] $method, [string] $url, array $fields = [] ) : [stirng]
$method [string]
$url [string]
$fields array [执行POST请求时的数据]
return [stirng]
    public static function request($method, $url, $fields = array())
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, self::genCookie());
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        }
        $result = curl_exec($ch);
        return $result;
    }

Usage Example

Example #1
0
 public function getToken()
 {
     $username = self::CONFIG_USERNAME;
     $password = self::CONFIG_PASSWORD;
     $time = time();
     $hash = md5("{$username}@{$password}@{$time}");
     $auth = "{$username}@{$time}@{$hash}";
     $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
     $url = 'http://axe.mappy.com/1v1/token/generate.aspx?' . 'auth=' . urlencode($auth) . '&' . 'ip=' . urldecode($ip);
     /**
             $remote = @fopen($url, 'rb');
     
             if ( false === $remote )
        return false;
     
             $token = '';
     
             while ( !( feof($remote) ) )
        $token .= fread($remote, 8192);
     
             fclose($remote);
     */
     $curl = new Curl($url);
     $curl->request();
     $resp = $curl->response();
     $curl->disconnect();
     return $resp['body'];
 }
All Usage Examples Of Curl::request