做了一个小的PHP脚本,需要测试它,但它默认不会发布任何东西。如何输出?

时间:2012-08-28 10:30:12

标签: php xml variables

我尝试使用echo $ steamrep_id,但没有运气,有人可以帮忙吗?代码:

<?php
class Steam_Rep_Rep {

    // cURL Variable
    private $ch = null;

    public function __construct() {
        // Setup cURL
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 4);
    }    

    public function getUserInfo($steamrep_id) {
        // cURL
        curl_setopt($this->ch, CURLOPT_URL, "http://steamrep.com/api/beta/reputation/{$steamrep_id}?json=1");
        $result = curl_exec($this->ch);
        $xml = simplexml_load_string($result);

        if(!empty($xml)) {
            return array(
                'rep' => $xml->steamREP,

            );
        } else {
            return array(
                'rep' => "No reputation found",
            );
        }
    }
}
?>

基本上,它从XML文件中获取数据并将其存储在变量中 还要注意我是一个半新手的编码,我只是把其他代码的一部分粘在一起使这个工作,我甚至不确定它是否能完成它的工作。 编辑:不,我不会执行变量,我只会创建一个从变量获取数据的代码,就是这样。

2 个答案:

答案 0 :(得分:0)

试试这个:

$test = new Steam_Rep_Rep();

$var = $test->getUserInfo("id_goes_here");

var_dump($var);

将它放在脚本的末尾,然后在浏览器中运行它。

答案 1 :(得分:0)

您有一些错误,请将STEAM_ID64替换为STEAM_ID,它应该可以正常工作

class Steam_Rep_Rep {

    // cURL Variable
    private $ch = null;

    public function __construct() {
        // Setup cURL
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 4);
    }    

    public function getUserInfo($steamrep_id) {
        // cURL
        curl_setopt($this->ch, CURLOPT_URL, "http://steamrep.com/api/beta/reputation/{$steamrep_id}?json=1");
        $result = curl_exec($this->ch);
        $xml = json_decode($result);
        if(!empty($xml->steamrep->reputation)) {
            return array(
                'rep' => $xml->steamrep->reputation,
            );
        } else {
            return array(
                'rep' => "No reputation found",
            );
        }
    }
}

$steam = new Steam_Rep_Rep();
$rep = $steam->getUserInfo('76561197971691194');
echo $rep['rep'];