任何人都可以帮我转换下面写的这个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
字符串:
答案 0 :(得分:0)
我认为这就是你要做的。请参阅我的笔记,您必须根据您的能力和要求取消注释两个选项中的一个:
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;
}