PHP - javascriptdan PHP klassiga parametrlarni qanday o'tkazish va javobni js-ga qaytarish

PHP klassini JavaScript bilan birgalikda ishlatishga harakat qilaman, lekin men buni parametrni qabul qilish uchun sinfni o'zgartirishim kerak va men PHP bilan tanish emasligim uchun buni qanday amalga oshirish kerakligiga ishonchim komil emas . Bu quyidagi sinfdir - siz statik o'zgaruvchan $ saytidan foydalanayotganini ko'rganingizdek, lekin sayt o'zgaruvchilaridan o'tishni xohlayman. Shuningdek, ma'lumotlarni qaytarib olishni xohlayman. Buni qanday qilishni har kim biladi?

<?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 . "
"; $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 "
HMAC: " . $sigHMAC; $sig64 = base64_encode($sigHMAC); echo "
BASE64: " . $sig64 . "
"; $sig = base64_encode(hash_hmac('sha256', $sign, $this->secretAccessKey, true)); echo "
ENCODED URL: " . rawurlencode($sig) ."
"; 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(); ?>
1

2 javoblar

Sinfni o'zgartirish kerak deb o'ylamayman. O'zingizni JS-dan o'zgaruvchan parametrlardan o'tishi mumkin bo'lgan va JSON hosil qiluvchi PHP-skriptni yaratib qo'ying. Masalan,

<?php
    $data = array();
    if ($_GET['ONE_PARAM'])
    {
        $url = new UrlInfo();
        $data = $url->YOUR_METHOD($_GET['ONE_PARAM']);
    }
    header('Content-type: application/json');
    echo(json_encode($data));
?>

Keyin JS-o'zgaruvchilaringizni AJAX orqali PHP-skriptingizga o'tkazasiz va u erda javobni o'qiysiz.

2
qo'shib qo'ydi

Siz konstruktoringizda ishlatadigan sinf o'zgaruvchilarini hech qachon aniqlamaysiz. Siz ularni sinfingizda e'lon qilishingiz kerak:

<?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';

   //Add your public class variables here
    public $accessKeyId;
    public $secretAccessKey;
    public $site;

    public function UrlInfo($accessKeyId, $secretAccessKey, $site) {
        $this->accessKeyId = $accessKeyId;
        $this->secretAccessKey = $secretAccessKey;
        $this->site = $site;
    }
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb

PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin