示例#1
0
 protected function sendAndReceive($request)
 {
     $f = new SaeFetchurl();
     $cookie = $this->getCookie();
     if ($cookie != '') {
         $f->setHeader("Cookie", $cookie);
     }
     if ($this->keepAlive) {
         $f->setHeader("Connection", "keep-alive");
         $f->setHeader("Keep-Alive", $this->keepAliveTimeout);
     } else {
         $f->setHeader("Connection", "close");
     }
     foreach ($this->header as $name => $value) {
         $f->setHeader($name, $value);
     }
     $f->setMethod("post");
     $f->setPostData($request);
     $f->setConnectTimeout($this->timeout);
     $f->setSendTimeout($this->timeout);
     $f->setReadTimeout($this->timeout);
     $response = $f->fetch($this->url);
     if ($f->errno()) {
         throw new Exception($f->errno() . ": " . $f->errmsg());
     }
     $http_response_header = $f->responseHeaders(false);
     $this->setCookie($http_response_header);
     return $response;
 }
示例#2
0
文件: lib.php 项目: coffiner/Layer
 public static function fetch_url($url)
 {
     switch (RUN_ENV) {
         case 'SAE':
             //使用SAE FetchURL服务
             $f = new SaeFetchurl();
             if (STATIC_HOST) {
                 $f->setHeader('Host', STATIC_HOST);
             }
             $content = $f->fetch($url);
             return array($f->HttpCode(), $content);
             break;
         case 'BAE':
         case 'LOCAL':
         default:
             if (function_exists('curl_init')) {
                 //BAE或普通平台下可使用curl
                 $ch = curl_init();
                 curl_setopt($ch, CURLOPT_URL, $url);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                 if (!ini_get('safe_mode')) {
                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                 }
                 if (STATIC_HOST) {
                     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . STATIC_HOST));
                 }
                 $con = curl_exec($ch);
                 $cod = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                 return array($cod, $con);
             } else {
                 //否则使用file_get_contents
                 $content = '';
                 if (STATIC_HOST) {
                     $opt = array('http' => array('header' => 'Host: ' . STATIC_HOST));
                     $context = stream_context_create($opt);
                     $content = file_get_contents($url, false, $context);
                 } else {
                     $content = file_get_contents($url);
                 }
                 list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);
                 return array($status_code, $content);
             }
     }
 }
示例#3
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (0 < count($arguments) || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
     }
     if ($byRef) {
         $hproseWriter->writeBoolean(true);
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     if ($this->filter) {
         $request = $this->filter->outputFilter($request);
     }
     $stream->close();
     $f = new SaeFetchurl();
     $cookie = $this->getCookie();
     if ($cookie != "") {
         $f->setHeader("Cookie", $cookie);
     }
     if ($this->keepAlive) {
         $f->setHeader("Connection", "keep-alive");
         $f->setHeader("Keep-Alive", $this->keepAliveTimeout);
     } else {
         $f->setHeader("Connection", "close");
     }
     foreach ($this->header as $name => $value) {
         $f->setHeader($name, $value);
     }
     $f->setMethod("post");
     $f->setPostData($request);
     $f->setConnectTimeout($this->timeout);
     $f->setSendTimeout($this->timeout);
     $f->setReadTimeout($this->timeout);
     $response = $f->fetch($this->url);
     if ($f->errno()) {
         throw new HproseException($f->errno() . ": " . $f->errmsg());
     }
     $http_response_header = $f->responseHeaders(false);
     $this->setCookie($http_response_header);
     if ($this->filter) {
         $response = $this->filter->inputFilter($response);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }
示例#4
0
<?php

/**
 * keepsession.php keep session执行
 * ----------------------------------------------------------------
 */
include '../init.php';
$db = DBConnect();
$rows = $db->Dataset("SELECT * FROM " . Tb('keepsession'));
$f = new SaeFetchurl();
foreach ($rows as $row) {
    if (time() > $row['updateTime'] + 300) {
        $f->setHeader("Cookie", urldecode($row['cookie']));
        $con = $f->fetch($row['url']);
        $db->Execute("UPDATE " . Tb('keepsession') . " SET updateTime='" . time() . "' WHERE id='{$row[id]}'");
    }
}
示例#5
0
if (isset($_POST['action'])) {
    $words = $_POST['words'];
    //$ch = curl_init();
    $url = 'http://apis.baidu.com/apistore/tranlateservice/translate?query=' . $words . '&from=en&to=zh';
    $header = array('apikey: 66b4ab19cc801359be7aa58a3f8bc787');
    // 添加apikey到header
    //curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    //curl_setopt($ch , CURLOPT_URL , $url);
    //$res = curl_exec($ch);*/
    //////////////////////////////
    //建立抓取对象
    $fetch = new SaeFetchurl();
    //$ak="66b4ab19cc801359be7aa58a3f8bc787";
    $flag = $fetch->setHeader("apikey", $header);
    $res = $fetch->fetch($url);
    //////////////////////////////
    echo $res . $flag;
    $result = json_decode($res, true);
    echo $result["retData"]["trans_result"]["0"]["dst"];
    echo '</br><a href="' . $_SERVER['PHP_SELF'] . '">try again</a>';
} else {
    ?>

<form action="translate.php" method="post">
	<input type="text" name="words"/> </br>
	<input type="hidden" name="action" value="submitted"/> </br>
	<input type="submit" name="submit" value="submit"/> </br>
</form>
<?php