我怎么能把这个js代码转移到PHP


How can I transfer this js code to PHP

谁能帮我把我写在下面的javascript脚本转换成PHP?如有任何帮助,不胜感激:

<script type="text/javascript">
    $.getJSON('inc/data.php',{format: "json"}, function(data) {
        var valC="";
        $.each(data['canales'], function(key, val) {
            var str = key;
            str = str.replace("_","-");
            valC=valC+'<li><a href="'+str+'.php" title="'+key+'"><img class="img-min-menu" src="http://cdn.vaughnsoft.com/vaughnsoft/vaughn/img_profiles/'+val+'_320.jpg" alt=""></a></li>';
        });
        $("#lista").html(valC);
    }); 
</script>

这个php文件从一个数组生成一个json字符串:

http://pastebin.com/xmqvii2X

我认为这就是你想要做的。请参阅我的注释,您必须根据您的功能和需求取消两个选项中的一个:

class getJSON
    {
        protected   $valC;
        protected   $getFile;
        public  function __construct($filename = 'inc/data.php')
            {
                $this->valC     =   "";
                // If you can edit page two, remove lines 65 down on that page and uncomment this:
                // $this->doOpt1($filename);
                // If you are stuck with page two uncomment this:
                // $this->doOpt2($filename);
            }
        private function doOpt1($filename)
            {
                // Include the page
                include_once($filename);
                // Just assign the array
                $this->getFile  =   $lista->canales;
            }
        private function doOpt2($filename)
            {
                // Create a buffer
                ob_start();
                // Include the page which echos the json string
                include($filename);
                // Capture the buffer
                $data   =   ob_get_contents();
                // Flush
                ob_end_clean();
                // Decode the json
                $this->getFile  =   json_decode(trim($data),true);
            }
        public  function createLinks()
            {
                if(empty($this->getFile) || !is_array($this->getFile))
                    return $this->valC;
                foreach($this->getFile as $key => $val) {
                        $this->valC .=  '
                    <li>
                        <a href="'.str_replace("_","-",$key).'.php" title="'.$key.'">
                            <img class="img-min-menu" src="http://cdn.vaughnsoft.com/vaughnsoft/vaughn/img_profiles/'.$val.'_320.jpg" alt="">
                        </a>
                    </li>';
                    }
                return $this->valC;
            }
    }           
$makeLinks  =   new getJSON();
?>
<div id="lista"><?php echo $makeLinks->createLinks(); ?></div>

编辑

经过进一步的讨论,听起来你想从原来的东西上刮下来。看看这是不是你想要做的。它需要一个cURL库和DOM库:

<?php
    class   cURL
        {
            public      $response;
            protected   $sendHeader;
            protected   $PostFields;
            private     $query;
            public  function    __construct($query = '')
                {
                    $this->sendHeader   =   false;
                    $this->query        =   $query;
                    if(!empty($this->query)) {
                            if(!is_array($this->query))
                                $this->response =   $this->Connect($this->query);
                            else
                                $this->encode();
                        }
                }
            public  function SendPost($array = array())
                {
                    $this->PostFields['payload']    =   $array;
                    $this->PostFields['query']      =   http_build_query($array);
                    return $this;
                }
            public  function Connect($_url,$deJSON = true)
                {
                    // Remote Connect
                    $ch         = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $_url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    if(strpos($_url,"https://") !== false) {
                            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,2);
                            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
                        }
                    if(!empty($this->PostFields['payload'])) {
                            curl_setopt($ch, CURLOPT_POST, count($this->PostFields['payload']));
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->PostFields['query']);
                        }
                    if(!empty($this->sendHeader))
                        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56');
                    $decode     =   curl_exec($ch);
                    $_response  =   ($deJSON)? json_decode($decode, true) : $decode;
                    $error      =   curl_error($ch);
                    curl_close($ch);
                    return (empty($error))? $_response: $error;
                }
            public  function emulateBrowser()
                {
                    $this->sendHeader   =   true;
                    return $this;
                }
            public  function encode($_filter = 0)
                {
                    foreach($this->query as $key => $value) {
                            $string[]   =   urlencode($key).'='.urlencode($value);
                        }
                    if($_filter == true)
                        $string =   array_filter($string);
                    return implode("&",$string);
                }
        }
使用

>

// Create cURL instance
$curl   =   new cURL();
// Masquerade as a browser and get contents
$val    =   $curl   ->emulateBrowser()
                    ->Connect('http://vaughnlive.tv/browse/espanol?a=mvn%22',false);
// Create a DOM instance
$DOM    =   new DOMDocument();
// Process the returned data
@$DOM->loadHTML($val);
// Fetch the <a> elements
$aArray =   $DOM->getElementsByTagName('a');
// Loop through them and echo the html
foreach ($aArray as $node) {
    echo $DOM->saveHtml($node), PHP_EOL;
}