我想與PHP一起使用PHP類,但我需要修改類以便能夠通過它參數,因爲我不太熟悉PHP,我不知道如何做到這一點。這是下面的類 - 正如你可以看到它使用一個靜態變量$ site,但我希望能夠傳入站點變量。我也希望能夠返回數據。有人知道怎麼做嗎?PHP的 - 如何從JavaScript傳遞參數到一個PHP類,並返回響應返回到js
<?php
/**
* Makes a request to AWIS for site info.
*/
class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = 'HmacSHA256';
public function UrlInfo($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
}
/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$queryParams = $this->buildQueryParams();
$sig = $this->generateSignature($queryParams);
$url = 'http://' . self::$ServiceHost . '/?' . $queryParams . '&Signature=' . $sig;
echo "\nURL: " . $url . "<br/>";
$ret = self::makeRequest($url);
echo "\nResults for " . $this->site .":\n\n";
self::parseResponse($ret);
}
/**
* Builds current ISO8601 timestamp.
*/
protected static function getTimestamp() {
return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
}
/**
* 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,
'ResponseGroup' => self::$ResponseGroupName,
'AWSAccessKeyId' => $this->accessKeyId,
'Timestamp' => self::getTimestamp(),
'Count' => self::$NumReturn,
'Start' => self::$StartNum,
'SignatureVersion' => self::$SigVersion,
'SignatureMethod' => self::$HashAlgorithm,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue[] = $k . '=' . rawurlencode($v);
}
$keyImplode = implode('&',$keyvalue);
return implode('&',$keyvalue);
}
/**
* Makes request to AWIS
* @param String $url URL to make request to
* @return String Result of request
*/
protected static function makeRequest($url) {
/*echo "\nMaking request to:\n$url\n";*/
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$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,null,false,
'http://awis.amazonaws.com/doc/2005-07-11');
if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
$info = $xml->Response->UrlInfoResult->Alexa;
$nice_array = array(
'Links In Count' => $info->ContentData->LinksInCount,
'Rank' => $info->TrafficData->Rank
);
}
foreach($nice_array as $k => $v) {
echo $k . ': ' . $v ."\n";
}
}
/**
* Generates an HMAC signature per RFC 2104.
*
* @param String $url URL to use in createing signature
*/
protected function generateSignature($url) {
$sign = "GET\n" . strtolower(self::$ServiceHost) . "\n/\n". $url;
echo "String to sign: \n" . $sign . "\n";
$sigHMAC = hash_hmac('sha256', $sign, $this->secretAccessKey, true);
echo "<br/>HMAC: " . $sigHMAC;
$sig64 = base64_encode($sigHMAC);
echo "<br/>BASE64: " . $sig64 . "<br/>";
$sig = base64_encode(hash_hmac('sha256', $sign, $this->secretAccessKey, true));
echo "<br/>ENCODED URL: " . rawurlencode($sig) ."<br/>";
return rawurlencode($sig);
}
}
/*if (count($argv) < 4) {
echo "Usage: $argv[0] ACCESS_KEY_ID SECRET_ACCESS_KEY site\n";
exit(-1);
}
else {
$accessKeyId = $argv[1];
$secretAccessKey = $argv[2];
$site = $argv[3];
}*/
$accessKeyId = "AKIAJA664T37BDNPSXXX";
$secretAccessKey = "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";
$site = "http://site.com";
$urlInfo = new UrlInfo($accessKeyId, $secretAccessKey, $site);
$urlInfo->getUrlInfo();
?>