本文整理汇总了PHP中Requests::flattern方法的典型用法代码示例。如果您正苦于以下问题:PHP Requests::flattern方法的具体用法?PHP Requests::flattern怎么用?PHP Requests::flattern使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Requests
的用法示例。
在下文中一共展示了Requests::flattern方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
/**
* Perform a request
*
* @throws Requests_Exception On a cURL error (`curlerror`)
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
* @return string Raw HTTP result
*/
public function request($url, $headers = array(), $data = array(), $options = array())
{
$options['hooks']->dispatch('curl.before_request', array(&$this->fp));
$headers = Requests::flattern($headers);
if (($options['type'] === Requests::HEAD || $options['type'] === Requests::GET) & !empty($data)) {
$url = self::format_get($url, $data);
}
switch ($options['type']) {
case Requests::POST:
curl_setopt($this->fp, CURLOPT_POST, true);
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::HEAD:
curl_setopt($this->fp, CURLOPT_NOBODY, true);
break;
}
curl_setopt($this->fp, CURLOPT_URL, $url);
curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_REFERER, $url);
curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
if (true === $options['blocking']) {
curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
}
$options['hooks']->dispatch('curl.before_send', array(&$this->fp));
if ($options['filename'] !== false) {
$stream_handle = fopen($options['filename'], 'wb');
curl_setopt($this->fp, CURLOPT_FILE, $stream_handle);
}
$response = curl_exec($this->fp);
$options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
if ($options['blocking'] === false) {
curl_close($this->fp);
$fake_headers = '';
$options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
return false;
}
if ($options['filename'] !== false) {
fclose($stream_handle);
$this->headers = trim($this->headers);
} else {
$this->headers .= $response;
}
if (curl_errno($this->fp) === 23 || curl_errno($this->fp) === 61) {
curl_setopt($this->fp, CURLOPT_ENCODING, 'none');
$this->headers = curl_exec($this->fp);
}
if (curl_errno($this->fp)) {
throw new Requests_Exception('cURL error ' . curl_errno($this->fp) . ': ' . curl_error($this->fp), 'curlerror', $this->fp);
return;
}
$this->info = curl_getinfo($this->fp);
curl_close($this->fp);
$options['hooks']->dispatch('curl.after_request', array(&$this->headers));
return $this->headers;
}
示例2: request
/**
* Perform a request
*
* @throws Requests_Exception On failure to connect to socket (`fsockopenerror`)
* @throws Requests_Exception On socket timeout (`timeout`)
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
* @return string Raw HTTP result
*/
public function request($url, $headers = array(), $data = array(), $options = array())
{
$options['hooks']->dispatch('fsockopen.before_request');
$url_parts = parse_url($url);
$host = $url_parts['host'];
if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') {
$host = 'ssl://' . $host;
$url_parts['port'] = 443;
}
if (!isset($url_parts['port'])) {
$url_parts['port'] = 80;
}
$fp = @fsockopen($host, $url_parts['port'], $errno, $errstr, $options['timeout']);
if (!$fp) {
throw new Requests_Exception($errstr, 'fsockopenerror');
return;
}
$request_body = '';
$out = '';
switch ($options['type']) {
case Requests::POST:
case Requests::PUT:
case Requests::PATCH:
if (isset($url_parts['path'])) {
$path = $url_parts['path'];
if (isset($url_parts['query'])) {
$path .= '?' . $url_parts['query'];
}
} else {
$path = '/';
}
$out = $options['type'] . " {$path} HTTP/1.0\r\n";
if (is_array($data)) {
$request_body = http_build_query($data, null, '&');
} else {
$request_body = $data;
}
if (empty($headers['Content-Length'])) {
$headers['Content-Length'] = strlen($request_body);
}
if (empty($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}
break;
case Requests::HEAD:
case Requests::GET:
case Requests::DELETE:
$get = self::format_get($url_parts, $data);
$out = $options['type'] . " {$get} HTTP/1.0\r\n";
break;
}
$out .= "Host: {$url_parts['host']}\r\n";
$out .= "User-Agent: {$options['useragent']}\r\n";
$accept_encoding = $this->accept_encoding();
if (!empty($accept_encoding)) {
$out .= "Accept-Encoding: {$accept_encoding}\r\n";
}
$headers = Requests::flattern($headers);
if (!empty($headers)) {
$out .= implode($headers, "\r\n") . "\r\n";
}
$options['hooks']->dispatch('fsockopen.after_headers', array(&$out));
if (substr($out, -2) !== "\r\n") {
$out .= "\r\n";
}
$out .= "Connection: Close\r\n\r\n" . $request_body;
$options['hooks']->dispatch('fsockopen.before_send', array(&$out));
fwrite($fp, $out);
$options['hooks']->dispatch('fsockopen.after_send', array(&$fake_headers));
if (!$options['blocking']) {
fclose($fp);
$fake_headers = '';
$options['hooks']->dispatch('fsockopen.after_request', array(&$fake_headers));
return '';
}
stream_set_timeout($fp, $options['timeout']);
$this->info = stream_get_meta_data($fp);
$this->headers = '';
$this->info = stream_get_meta_data($fp);
if (!$options['filename']) {
while (!feof($fp)) {
$this->info = stream_get_meta_data($fp);
if ($this->info['timed_out']) {
throw new Requests_Exception('fsocket timed out', 'timeout');
}
$this->headers .= fread($fp, 1160);
}
} else {
//.........这里部分代码省略.........
示例3: setup_handle
/**
* Setup the cURL handle for the given data
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
*/
protected function setup_handle($url, $headers, $data, $options)
{
$options['hooks']->dispatch('curl.before_request', array(&$this->fp));
$headers = Requests::flattern($headers);
if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
$url = self::format_get($url, $data);
}
switch ($options['type']) {
case Requests::POST:
curl_setopt($this->fp, CURLOPT_POST, true);
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::PATCH:
case Requests::PUT:
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::DELETE:
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case Requests::HEAD:
curl_setopt($this->fp, CURLOPT_NOBODY, true);
break;
}
curl_setopt($this->fp, CURLOPT_URL, $url);
curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_REFERER, $url);
curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
if (true === $options['blocking']) {
curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
}
}