本文整理汇总了PHP中fsockopen函数的典型用法代码示例。如果您正苦于以下问题:PHP fsockopen函数的具体用法?PHP fsockopen怎么用?PHP fsockopen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fsockopen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendData
protected static function sendData($host, $POST, $HEAD, $filepath, $mediafile, $TAIL)
{
$sock = fsockopen("ssl://" . $host, 443);
fwrite($sock, $POST);
fwrite($sock, $HEAD);
//write file data
$buf = 1024;
$totalread = 0;
$fp = fopen($filepath, "r");
while ($totalread < $mediafile['filesize']) {
$buff = fread($fp, $buf);
fwrite($sock, $buff, $buf);
$totalread += $buf;
}
//echo $TAIL;
fwrite($sock, $TAIL);
sleep(1);
$data = fgets($sock, 8192);
$data .= fgets($sock, 8192);
$data .= fgets($sock, 8192);
$data .= fgets($sock, 8192);
$data .= fgets($sock, 8192);
$data .= fgets($sock, 8192);
$data .= fgets($sock, 8192);
fclose($sock);
list($header, $body) = preg_split("/\\R\\R/", $data, 2);
$json = json_decode($body);
if (!is_null($json)) {
return $json;
}
return false;
}
示例2: _recaptcha_http_post
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host, $path, $data, $port = 80)
{
$req = _recaptcha_qsencode($data);
$proxy_host = "proxy.iiit.ac.in";
$proxy_port = "8080";
$http_request = "POST http://{$host}{$path} HTTP/1.0\r\n";
$http_request .= "Host: {$host}\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if (false == ($fs = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 10))) {
die('Could not open socket aah');
}
fwrite($fs, $http_request);
while (!feof($fs)) {
$response .= fgets($fs, 1160);
}
// One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
示例3: sockPost
/**
* http post request
*
* @codeCoverageIgnore
*
* @param $url
* @param array $query
* @param $port
*
* @return mixed
*/
public static function sockPost($url, $query, $port = 80)
{
$data = '';
$info = parse_url($url);
$fp = fsockopen($info['host'], $port, $errno, $errstr, 30);
if (!$fp) {
return $data;
}
$head = 'POST ' . $info['path'] . " HTTP/1.0\r\n";
$head .= 'Host: ' . $info['host'] . "\r\n";
$head .= 'Referer: http://' . $info['host'] . $info['path'] . "\r\n";
$head .= "Content-type: application/x-www-form-urlencoded\r\n";
$head .= 'Content-Length: ' . strlen(trim($query)) . "\r\n";
$head .= "\r\n";
$head .= trim($query);
$write = fwrite($fp, $head);
$header = '';
while ($str = trim(fgets($fp, 4096))) {
$header .= $str;
}
while (!feof($fp)) {
$data .= fgets($fp, 4096);
}
return $data;
}
示例4: sendMemcacheCommand
function sendMemcacheCommand($server, $port, $command)
{
$s = @fsockopen($server, $port);
if (!$s) {
die("Cant connect to:" . $server . ':' . $port);
}
fwrite($s, $command . "\r\n");
$buf = '';
while (!feof($s)) {
$buf .= fgets($s, 256);
if (strpos($buf, "END\r\n") !== false) {
// stat says end
break;
}
if (strpos($buf, "DELETED\r\n") !== false || strpos($buf, "NOT_FOUND\r\n") !== false) {
// delete says these
break;
}
if (strpos($buf, "OK\r\n") !== false) {
// flush_all says ok
break;
}
}
fclose($s);
return parseMemcacheResults($buf);
}
示例5: downloadToString
function downloadToString()
{
$crlf = "\r\n";
// generate request
$req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf . 'Host: ' . $this->_host . $crlf . $crlf;
// fetch
$this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
fwrite($this->_fp, $req);
while (is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) {
$response .= fread($this->_fp, 1024);
}
fclose($this->_fp);
// split header and body
$pos = strpos($response, $crlf . $crlf);
if ($pos === false) {
return $response;
}
$header = substr($response, 0, $pos);
$body = substr($response, $pos + 2 * strlen($crlf));
// parse headers
$headers = array();
$lines = explode($crlf, $header);
foreach ($lines as $line) {
if (($pos = strpos($line, ':')) !== false) {
$headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1));
}
}
// redirection?
if (isset($headers['location'])) {
$http = new ilHttpRequest($headers['location']);
return $http->DownloadToString($http);
} else {
return $body;
}
}
示例6: http_send
function http_send($_url, $_body)
{
$errno = 0;
$errstr = '';
$timeout = 10;
$fp = fsockopen(_IP_, _PORT_, $errno, $errstr, $timeout);
if (!$fp) {
return FALSE;
}
$_head = "POST /" . $_url . " HTTP/1.1\r\n";
$_head .= "Host: " . _IP_ . ":" . _PORT_ . "\r\n";
$_head .= "Content-Type: Text/plain\r\n";
if (!$_body) {
$body_len = 0;
} else {
$body_len = strlen($_body);
}
$send_pkg = $_head . "Content-Length:" . $body_len . "\r\n\r\n" . $_body;
ilog(iLOG_INFO, " -----> http_send url: " . $_url);
ilog(iLOG_INFO, " -----> http_send body: " . $_body);
if (fputs($fp, $send_pkg) === FALSE) {
return FALSE;
}
//设置3s超时
stream_set_timeout($fp, 3);
while (!feof($fp)) {
ilog(iLOG_INFO, " -----> rsp: " . fgets($fp, 128));
}
if ($fp) {
fclose($fp);
}
return TRUE;
}
示例7: send_request_via_fsockopen1
function send_request_via_fsockopen1($host, $path, $content)
{
$posturl = "ssl://" . $host;
$header = "Host: {$host}\r\n";
$header .= "User-Agent: PHP Script\r\n";
$header .= "Content-Type: text/xml\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = fsockopen($posturl, 443, $errno, $errstr, 30);
if (!$fp) {
$response = false;
} else {
error_reporting(E_ERROR);
fputs($fp, "POST {$path} HTTP/1.1\r\n");
fputs($fp, $header . $content);
fwrite($fp, $out);
$response = "";
while (!feof($fp)) {
$response = $response . fgets($fp, 128);
}
fclose($fp);
error_reporting(E_ALL ^ E_NOTICE);
}
return $response;
}
示例8: __construct
/**
* Construct a new NetworkPrintConnector
*
* @param string $ip IP address or hostname to use.
* @param string $port The port number to connect on.
* @throws Exception Where the socket cannot be opened.
*/
public function __construct($ip, $port = "9100")
{
$this->fp = @fsockopen($ip, $port, $errno, $errstr);
if ($this->fp === false) {
throw new Exception("Cannot initialise NetworkPrintConnector: " . $errstr);
}
}
示例9: n2k_request_response
function n2k_request_response($request, $description = 'N2K')
{
$errno = 0;
$errstr = '';
$n2k = @fsockopen('localhost', 2597, $errno, $errstr, 15);
if (!$n2k) {
echo "Cannot connect to N2KD: {$errstr}\n";
exit(1);
}
#
# Ask for device list
#
if ($request !== null) {
fwrite($n2k, $request . "\n");
}
$s = '';
while (!feof($n2k)) {
$s .= fgets($n2k, 1024);
}
fclose($n2k);
$data = json_decode($s, true);
if (!is_array($data)) {
echo "Error: received invalid response for {$description} request\n";
exit(1);
}
return $data;
}
示例10: openstats
function openstats()
{
$fp = fsockopen($this->host, $this->port, $errno, $errstr, 10);
if (!$fp) {
$this->_error = "{$errstr} ({$errno})";
return 0;
} else {
fputs($fp, "GET /admin.cgi?pass=" . $this->passwd . "&mode=viewxml HTTP/1.0\r\n");
fputs($fp, "User-Agent: Mozilla\r\n\r\n");
while (!feof($fp)) {
$this->_xml .= fgets($fp, 512);
}
fclose($fp);
if (stristr($this->_xml, "HTTP/1.0 200 OK") == true) {
// <-H> Thanks to Blaster for this fix.. trim();
$this->_xml = trim(substr($this->_xml, 42));
} else {
$this->_error = "Bad login";
return 0;
}
$xmlparser = xml_parser_create();
if (!xml_parse_into_struct($xmlparser, $this->_xml, $this->_values, $this->_indexes)) {
$this->_error = "Unparsable XML";
return 0;
}
xml_parser_free($xmlparser);
return 1;
}
}
示例11: __construct
/**
* Constructor.
*
* @access public
* @param string $host Redis host
* @param string $port Redis port
*/
public function __construct($host, $port = 6379)
{
$this->connection = @fsockopen('tcp://' . $host, $port, $errNo, $errStr);
if (!$this->connection) {
throw new RedisException(vsprintf("%s(): %s", [__METHOD__, $errStr]), (int) $errNo);
}
}
示例12: DoTest
function DoTest($testname, $param, $hostname, $timeout, $params)
{
echo "Called for " . $hostname . " port " . $param . " timeout " . $timeout . "\n";
$timer = new TFNTimer();
$ip = ip_lookup($hostname);
echo $hostname . " => " . $ip . "\n";
if ($ip == "0") {
return -2;
}
// lookup failed
echo "Lookup Successful\n";
$errno = 0;
$errstr = "";
$timer->Start();
echo "Doing fsockopen()\n";
$fp = @fsockopen($ip, $param, $errno, $errstr, $timeout);
$elapsed = $timer->Stop();
echo "FP is : ";
echo $fp;
echo "\n";
if ($fp === false) {
return -1;
}
// open failed
echo "Closing\n";
@fclose($fp);
return $elapsed;
}
示例13: get
function get()
{
if (!empty($this->url)) {
$fp = fsockopen($this->host, 80, $errno, $errstr, 30);
if (!$fp) {
$this->status = false;
//$this->error['error_number'] = $errno;
//$this->error['error_msg'] = $errstr;
} else {
// here, we use double quotes to replace single quotes, or 400 error.
fputs($fp, 'GET ' . $this->path . " HTTP/1.1\r\n");
fputs($fp, 'User-Agent: ' . $this->user_agent . "\r\n");
fputs($fp, 'Host: ' . $this->host . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
while (!feof($fp)) {
$line = fgets($fp);
$this->content .= $line;
}
fclose($fp);
if (preg_match('/sogourank=(\\d+)/', $this->content, $matches) > 0) {
$this->response .= $matches[1];
}
}
} else {
$this->status = false;
}
}
示例14: request
public function request()
{
if ($this->_uri === null) {
throw new Modela_Exception("uri is not valid");
}
$sock = @fsockopen($this->_uriParts["host"], $this->_uriParts["port"]);
if (!$sock) {
throw new Modela_Exception('unable to open socket');
}
$requestString = $this->_method . " " . $this->_uriParts["path"];
if ($this->_uriParts["query"]) {
$requestString .= "?" . $this->_uriParts["query"];
}
$socketData = $requestString . self::HTTP_CRLF;
if ($this->_data) {
$socketData .= "Content-length: " . strlen($this->_data) . self::HTTP_CRLF;
$socketData .= "Content-type: application/json" . self::HTTP_CRLF;
$socketData .= "Connection: close" . self::HTTP_CRLF;
$socketData .= self::HTTP_CRLF;
$socketData .= $this->_data . self::HTTP_CRLF;
}
$socketData .= self::HTTP_CRLF . self::HTTP_CRLF;
fwrite($sock, $socketData);
$output = '';
$output .= stream_get_contents($sock);
list($this->_headers, $this->_response) = explode("\r\n\r\n", $output);
$this->_response = trim($this->_response);
fclose($sock);
return $this->_response;
}
示例15: postSMS
private static function postSMS($http, $data)
{
$post = '';
$row = parse_url($http);
$host = $row['host'];
$port = !empty($row['port']) ? $row['port'] : 80;
$file = $row['path'];
while (list($k, $v) = each($data)) {
$post .= rawurlencode($k) . "=" . rawurlencode($v) . "&";
}
$post = substr($post, 0, -1);
$len = strlen($post);
$fp = @fsockopen($host, $port, $errno, $errstr, 10);
// var_dump($fp);exit;
if (!$fp) {
return "{$errstr} ({$errno})\n";
} else {
$receive = '';
$out = "POST {$file} HTTP/1.0\r\n";
$out .= "Host: {$host}\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Connection: Close\r\n";
$out .= "Content-Length: {$len}\r\n\r\n";
$out .= $post;
fwrite($fp, $out);
while (!feof($fp)) {
$receive .= fgets($fp, 128);
}
fclose($fp);
$receive = explode("\r\n\r\n", $receive);
unset($receive[0]);
// var_dump($receive);exit;
return implode("", $receive);
}
}