在类/函数之外使用变量

时间:2018-11-21 12:04:33

标签: php class

我正在尝试在函数和类之外使用$rank变量。

class UrlInfo {
protected static $ActionName        = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost      = 'awis.amazonaws.com';
protected static $ServiceEndpoint  = 'awis.us-west-1.amazonaws.com';
protected static $NumReturn         = 10;
protected static $StartNum          = 1;
protected static $SigVersion        = '2';
protected static $HashAlgorithm     = '******';
protected static $ServiceURI = "/api";
protected static $ServiceRegion = "us-west-1";
protected static $ServiceName = "awis";


public function __construct($accessKeyId, $secretAccessKey, $site) {
    $this->accessKeyId = $accessKeyId;
    $this->secretAccessKey = $secretAccessKey;
    $this->site = $site;
    $now = time();
    $this->amzDate = gmdate("Ymd\THis\Z", $now);
    $this->dateStamp = gmdate("Ymd", $now);

}

/**
 * Get site info from AWIS.
 */
public function getUrlInfo() {
    $canonicalQuery = $this->buildQueryParams();
    $canonicalHeaders =  $this->buildHeaders(true);
    $signedHeaders = $this->buildHeaders(false);
    $payloadHash = hash('sha256', "");
    $canonicalRequest = "GET" . "\n" . self::$ServiceURI . "\n" . $canonicalQuery . "\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . $payloadHash;
    $algorithm = "AWS4-HMAC-SHA256";
    $credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
    $stringToSign = $algorithm . "\n" .  $this->amzDate . "\n" .  $credentialScope . "\n" .  hash('sha256', $canonicalRequest);
    $signingKey = $this->getSignatureKey();
    $signature = hash_hmac('sha256', $stringToSign, $signingKey);
    $authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' .  'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;

    $url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
    $ret = self::makeRequest($url, $authorizationHeader);
    // echo "\nResults for " . $this->site .":\n\n";
    // echo $ret;
    self::parseResponse($ret);
}

protected function sign($key, $msg) {
    return hash_hmac('sha256', $msg, $key, true);
}

protected function getSignatureKey() {
    $kSecret = 'AWS4' . $this->secretAccessKey;
    $kDate = $this->sign($kSecret, $this->dateStamp);
    $kRegion = $this->sign($kDate, self::$ServiceRegion);
    $kService = $this->sign($kRegion, self::$ServiceName);
    $kSigning = $this->sign($kService, 'aws4_request');
    return $kSigning;
}

/**
 * Builds headers for the request to AWIS.
 * @return String headers for the request
 */
protected function buildHeaders($list) {
    $params = array(
        'host'            => self::$ServiceEndpoint,
        'x-amz-date'      => $this->amzDate
    );
    ksort($params);
    $keyvalue = array();
    foreach($params as $k => $v) {
        if ($list)
          $keyvalue[] = $k . ':' . $v;
        else {
          $keyvalue[] = $k;
        }
    }
    return ($list) ? implode("\n",$keyvalue) . "\n" : implode(';',$keyvalue) ;
}

/**
 * Builds query parameters for the request to AWIS.
 * Parameter names will be in alphabetical order and
 * parameter values will be urlencoded per RFC 3986.
 * @return String query parameters for the request
 */
protected function buildQueryParams() {
    $params = array(
        'Action'            => self::$ActionName,
        'Count'             => self::$NumReturn,
        'ResponseGroup'     => self::$ResponseGroupName,
        'Start'             => self::$StartNum,
        'Url'               => $this->site
    );
    ksort($params);
    $keyvalue = array();
    foreach($params as $k => $v) {
        $keyvalue[] = $k . '=' . rawurlencode($v);
    }
    return implode('&',$keyvalue);
}

/**
 * Makes request to AWIS
 * @param String $url   URL to make request to
 * @param String authorizationHeader  Authorization string
 * @return String       Result of request
 */
protected function makeRequest($url, $authorizationHeader) {
    // echo "\nMaking request to:\n$url\n";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Accept: application/xml',
      'Content-Type: application/xml',
      'X-Amz-Date: ' . $this->amzDate,
      'Authorization: ' . $authorizationHeader
    ));
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

/**
 * Parses XML response from AWIS and displays selected data
 * @param String $response    xml response from AWIS
 */

public static function parseResponse($response) {
    $xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
    if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
        $info = $xml->Response->UrlInfoResult->Alexa;
        $links = $info->ContentData->LinksInCount;
        $rank = $info->TrafficData->Rank;
        echo "<br>Links in Count: " .$links;
        echo "<br>Rank: " .$rank;

        );
    }
   }
}

}

有人有想法吗? 谢谢!

我想将$ rank添加到数组中

      $arrayinfo = array(
      "domain" => $ip,
      "ip" => $ipphp,
      "Rank" => $rank,);

1 个答案:

答案 0 :(得分:0)

如果您将$ rank声明为静态变量(如以下示例的$ name)怎么办

<?php
class Test{
  static $name;
  static function setName($name){
    self::$name=$name;
  }

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name