本文整理汇总了PHP中stream_context_create函数的典型用法代码示例。如果您正苦于以下问题:PHP stream_context_create函数的具体用法?PHP stream_context_create怎么用?PHP stream_context_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stream_context_create函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: do_post_request
function do_post_request($url, $postdata, $files = NULL)
{
$data = "";
$boundary = "---------------------" . substr(md5(rand(0, 32000)), 0, 10);
if (is_array($postdata)) {
foreach ($postdata as $key => $val) {
$data .= "--" . $boundary . "\n";
$data .= "Content-Disposition: form-data; name=" . $key . "\n\n" . $val . "\n";
}
}
$data .= "--" . $boundary . "\n";
if (is_array($files)) {
foreach ($files as $key => $file) {
$fileContents = file_get_contents($file['tmp_name']);
$data .= "Content-Disposition: form-data; name=" . $key . "; filename=" . $file['name'] . "\n";
$data .= "Content-Type: application/x-bittorrent\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents . "\n";
$data .= "--" . $boundary . "--\n";
}
}
$params = array('http' => array('method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary=' . $boundary, 'content' => $data));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with " . $url . ", " . $php_errormsg);
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from " . $url . ", " . $php_errormsg);
}
return $response;
}
示例2: _test_environment_getHello
function _test_environment_getHello($url)
{
$fp = fopen($url, 'r', false, stream_context_create(array('http' => array('method' => "GET", 'timeout' => '10', 'header' => "Accept-Encoding: deflate, gzip\r\n"))));
$meta = stream_get_meta_data($fp);
$encoding = '';
$length = 0;
foreach ($meta['wrapper_data'] as $i => $header) {
if (preg_match('@^Content-Length:\\s*(\\d+)$@i', $header, $m)) {
$length = $m[1];
} elseif (preg_match('@^Content-Encoding:\\s*(\\S+)$@i', $header, $m)) {
if ($m[1] !== 'identity') {
$encoding = $m[1];
}
}
}
$streamContents = stream_get_contents($fp);
fclose($fp);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
if ($length != 6) {
echo "\nReturned content should be 6 bytes and not HTTP encoded.\n" . "Headers returned by: {$url}\n\n";
var_export($meta['wrapper_data']);
echo "\n\n";
}
}
return array('length' => $length, 'encoding' => $encoding, 'bytes' => $streamContents);
}
示例3: do_post_request
function do_post_request($url, $res, $file, $name)
{
$data = "";
$boundary = "---------------------" . substr(md5(rand(0, 32000)), 0, 10);
$data .= "--{$boundary}\n";
$fileContents = file_get_contents($file);
$md5 = md5_file($file);
$ext = pathinfo($file, PATHINFO_EXTENSION);
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"file.php\"\n";
$data .= "Content-Type: text/plain\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents . "\n";
$data .= "--{$boundary}--\n";
$params = array('http' => array('method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary=' . $boundary, 'content' => $data));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Erreur !");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Erreur !");
} else {
echo "file should be here : ";
/* LETTERBOX */
if (count($response) > 1) {
echo $response;
} else {
echo "<a href='" . $res . "tmp/tmp_file_" . $name . "." . $ext . "'>BACKDOOR<a>";
}
}
}
示例4: getStreamContext
/**
* @param bool $testEnv
* @param string $caFile
* @return resource
*/
private function getStreamContext($testEnv = false, $caFile = null)
{
if ($testEnv) {
$host = $this->certificatePeerName['test'];
} else {
$host = $this->certificatePeerName['production'];
}
$this->streamContextOption['ssl']['crypto_method'] = STREAM_CRYPTO_METHOD_TLS_CLIENT;
$this->streamContextOption['ssl']['verify_peer'] = true;
$this->streamContextOption['ssl']['SNI_enabled'] = true;
// Disable TLS compression to prevent CRIME attacks where supported (PHP 5.4.13 or later).
if (PHP_VERSION_ID >= 50413) {
$this->streamContextOption['ssl']['disable_compression'] = true;
}
if (PHP_VERSION_ID < 50600) {
//CN_match was deprecated in favour of peer_name in PHP 5.6
$this->streamContextOption['ssl']['CN_match'] = $host;
$this->streamContextOption['ssl']['SNI_server_name'] = $host;
// PHP 5.6 or greater will find the system cert by default. When < 5.6, use the system ca-certificates.
if (is_null($caFile)) {
$this->streamContextOption['ssl']['cafile'] = $this->getDefaultCABundle();
} else {
$this->streamContextOption['ssl']['cafile'] = $caFile;
}
} else {
$this->streamContextOption['ssl']['peer_name'] = $host;
$this->streamContextOption['ssl']['verify_peer_name'] = true;
}
return stream_context_create($this->streamContextOption);
}
示例5: _request
private function _request($request)
{
$request = array_map('Comet_encode', $request);
array_unshift($request, $this->ORIGIN);
$ctx = stream_context_create(array('http' => array('timeout' => 200)));
return json_decode(file_get_contents_curl(implode('/', $request)), true);
}
示例6: get_profile_email
function get_profile_email($url)
{
global $g_acegi_cookie;
global $g_session_cookie;
global $g_delay;
sleep($g_delay);
// Create a stream
// get cookie values using wget commands
$cookie = sprintf("Cookie: ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=%s; JSESSIONID=%s\r\n", $g_acegi_cookie, $g_session_cookie);
$opts = array('http' => array('method' => "GET", 'header' => "Accept-language: en\r\n" . $cookie));
$context = stream_context_create($opts);
@($html = file_get_contents($url, false, $context));
$doc = new \DOMDocument();
@$doc->loadHTML($html);
$emailNode = $doc->getElementById("email1__ID__");
$nameNode = $doc->getElementById("name1__ID__");
$titleNode = $doc->getElementById("title1__ID__");
$companyNode = $doc->getElementById("company1__ID__");
if (!is_null($emailNode)) {
$data = array();
$data["email"] = $emailNode->nodeValue;
$data["name"] = is_null($nameNode) ? "" : $nameNode->nodeValue;
$data["title"] = is_null($titleNode) ? "" : $titleNode->nodeValue;
$data["company"] = is_null($companyNode) ? "" : $companyNode->nodeValue;
return $data;
}
return NULL;
}
示例7: __call
public function __call($method, $arguments)
{
$dom = new DOMDocument('1.0', 'UTF-8');
$element = $dom->createElement('method');
$element->setAttribute('name', $method);
foreach ($arguments as $argument) {
$child = $dom->createElement('argument');
$textNode = $dom->createTextNode($argument);
$child->appendChild($textNode);
$element->appendChild($child);
}
$dom->appendChild($element);
$data = 'service=' . $dom->saveXML();
$params = array('http' => array('method' => 'POST', 'content' => $data));
$ctx = stream_context_create($params);
$fp = @fopen($this->server, 'rb', false, $ctx);
if (!$fp) {
throw new Exception('Problem with URL');
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from {$this->server}");
}
$dom = new DOMDocument(null, 'UTF-8');
$dom->loadXML($response);
$result = $dom->childNodes->item(0)->childNodes->item(0)->nodeValue;
$type = $dom->childNodes->item(0)->childNodes->item(1)->nodeValue;
settype($result, $type);
return $result;
}
示例8: sendOAuthBodyPOST
function sendOAuthBodyPOST($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $body)
{
$hash = base64_encode(sha1($body, TRUE));
$parms = array('oauth_body_hash' => $hash);
$test_token = '';
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
$acc_req->sign_request($hmac_method, $test_consumer, $test_token);
// Pass this back up "out of band" for debugging
global $LastOAuthBodyBaseString;
$LastOAuthBodyBaseString = $acc_req->get_signature_base_string();
// echo($LastOAuthBodyBaseString."\m");
$header = $acc_req->to_header();
$header = $header . "\r\nContent-type: " . $content_type . "\r\n";
$params = array('http' => array('method' => 'POST', 'content' => $body, 'header' => $header));
try {
$ctx = stream_context_create($params);
$fp = @fopen($endpoint, 'rb', false, $ctx);
} catch (Exception $e) {
$fp = false;
}
if ($fp) {
$response = @stream_get_contents($fp);
} else {
// Try CURL
$headers = explode("\r\n", $header);
$response = sendXmlOverPost($endpoint, $body, $headers);
}
if ($response === false) {
throw new Exception("Problem reading data from {$endpoint}, {$php_errormsg}");
}
return $response;
}
示例9: __construct
public function __construct($connProviderWsdlUri, $nsaProvider, $nsaRequester, $callBackServerUri = null, LocalCertificate $certificate = null)
{
if ($nsaProvider == null || $nsaRequester == null) {
throw new \Exception('NSA provider and NSA requester must be informed');
}
$this->nsaProvider = $nsaProvider;
$this->nsaRequester = $nsaRequester;
if ($certificate != null && !$certificate instanceof LocalCertificate) {
throw new \Exception('Certificate must be informed like a LocalCertificate object');
}
$this->localCert = $certificate;
$this->connProviderWsdlUri = $connProviderWsdlUri;
$this->callbackServerUri = $callBackServerUri;
$isSsl = $this->localCert == null ? false : true;
$streamContextOptions = ['ssl' => ['verify_peer' => $isSsl, 'allow_self_signed' => true], 'https' => ['curl_verify_ssl_peer' => $isSsl, 'curl_verify_ssl_host' => $isSsl]];
if (!$isSsl) {
$streamContextOptions['ssl']['ciphers'] = 'SHA1';
}
$context = stream_context_create($streamContextOptions);
$soapOptions = ['cache_wsdl' => WSDL_CACHE_NONE, 'stream_context' => $context, 'trace' => 1];
if ($isSsl) {
$soapOptions['local_cert'] = $this->localCert->getCertPath();
$soapOptions['passphrase'] = $this->localCert->getCertPassphrase();
}
parent::__construct($this->connProviderWsdlUri, $soapOptions);
}
示例10: send
/**
* Sends a JSON-RPC request
* @param Tivoka_Request $request A Tivoka request
* @return Tivoka_Request if sent as a batch request the BatchRequest object will be returned
*/
public function send($request)
{
if (func_num_args() > 1) {
$request = func_get_args();
}
if (is_array($request)) {
$request = Tivoka::createBatch($request);
}
if (!$request instanceof Tivoka_Request) {
throw new Tivoka_Exception('Invalid data type to be sent to server');
}
$parse = parse_url($this->target);
// preparing connection...
$context = stream_context_create(array($parse['scheme'] => array('content' => (string) $request, 'header' => "Content-Type: application/json\r\n" . "Connection: Close\r\n", 'method' => 'POST', 'timeout' => 10.0)));
//sending...
if ($this->oauth_consumer) {
$oauth = new OAuth_Request($this->oauth_consumer, 'POST', $this->target, array(), (string) $request);
$response = $oauth->request($context);
} else {
$response = @file_get_contents($this->target, false, $context);
}
if ($response === FALSE) {
throw new Tivoka_Exception('Connection to "' . $this->target . '" failed', Tivoka::ERR_CONNECTION_FAILED);
}
$request->setResponse($response);
return $request;
}
示例11: getHTTPContent
/**
* Get the content at the given URL using an HTTP GET call.
*
* @access public
* @static
* @param $url URL of the content.
* @return string Content at the given URL, false otherwise.
*/
public static function getHTTPContent($url)
{
// Sanity check
if (empty($url)) {
return false;
}
// Create the HTTP options for the HTTP stream context, see below
// Set phpMyFAQ agent related data
$agent = 'phpMyFAQ/' . PMF_Configuration::getInstance()->get('main.currentVersion') . ' on PHP/' . PHP_VERSION;
$opts = array('header' => 'User-Agent: ' . $agent . "\r\n", 'method' => 'GET');
// HTTP 1.1 Virtual Host
$urlParts = @parse_url($url);
if (isset($urlParts['host'])) {
$opts['header'] = $opts['header'] . 'Host: ' . $urlParts['host'] . "\r\n";
}
// Socket timeout
if (version_compare(PHP_VERSION, '5.2.1', '<')) {
@ini_set('default_socket_timeout', 5);
} else {
$opts['timeout'] = 5;
}
// Create the HTTP stream context
$ctx = stream_context_create(array('http' => $opts));
return file_get_contents($url, $flags, $ctx);
}
示例12: MakeAsyncPostRequest
public function MakeAsyncPostRequest($postData)
{
$content = http_build_query($postData, '', '&');
$context = stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded;', 'content' => $content)));
$handle = @fopen($this->uri, 'rb', false, $context);
return $handle != false;
}
示例13: post
protected function post($method, $parameters = [])
{
$options = ["ssl" => ["verify_peer" => false, "verify_peer_name" => false], 'http' => ['header' => "Content-type: application/json; charset=utf-8\r\n" . "ApiToken: " . $this->parameters['apitoken'] . "\r\n", 'method' => "POST", 'content' => json_encode($this->getApiParameters($parameters))]];
$context = stream_context_create($options);
$result = file_get_contents($this->url . $method, false, $context);
return $this->parseResult($result);
}
示例14: getFastestRealTime
/**
* @param $hosts
* @return string
*/
public static function getFastestRealTime($hosts)
{
$mint = 60.0;
$s_url = '';
$count_hosts = count($hosts);
for ($i = 0; $i < $count_hosts; $i++) {
$start = microtime(true);
$opts = array('http' => array('method' => 'GET', 'timeout' => 3));
$context = stream_context_create($opts);
$homepage = null;
try {
$homepage = file_get_contents($hosts[$i], false, $context);
} catch (\Exception $e) {
echo $e;
}
$ends = microtime(true);
if ($homepage === null || $homepage === '') {
$diff = 60.0;
} else {
$diff = $ends - $start;
}
if ($mint > $diff) {
$mint = $diff;
$s_url = $hosts[$i];
}
}
return $s_url;
}
示例15: request
/**
* Execute a HTTP request to the remote server
*
* Returns the result from the remote server.
*
* @param string $method
* @param string $path
* @param \eZ\Publish\Core\REST\Common\Message $message
*
* @return \eZ\Publish\Core\REST\Common\Message
*/
public function request($method, $path, Message $message = null)
{
$message = $message ?: new Message();
$requestHeaders = $this->getRequestHeaders($message->headers);
$url = $this->server . $path;
$contextOptions = array('http' => array('method' => $method, 'content' => $message->body, 'ignore_errors' => true, 'header' => $requestHeaders, 'follow_location' => 0));
$httpFilePointer = @fopen($url, 'r', false, stream_context_create($contextOptions));
// Check if connection has been established successfully
if ($httpFilePointer === false) {
throw new ConnectionException($this->server, $path, $method);
}
// Read request body
$body = '';
while (!feof($httpFilePointer)) {
$body .= fgets($httpFilePointer);
}
$metaData = stream_get_meta_data($httpFilePointer);
// This depends on PHP compiled with or without --curl-enable-streamwrappers
$rawHeaders = isset($metaData['wrapper_data']['headers']) ? $metaData['wrapper_data']['headers'] : $metaData['wrapper_data'];
$headers = array();
foreach ($rawHeaders as $lineContent) {
// Extract header values
if (preg_match('(^HTTP/(?P<version>\\d+\\.\\d+)\\s+(?P<status>\\d+))S', $lineContent, $match)) {
$headers['version'] = $match['version'];
$headers['status'] = (int) $match['status'];
} else {
list($key, $value) = explode(':', $lineContent, 2);
$headers[$key] = ltrim($value);
}
}
return new Message($headers, $body, $headers['status']);
}