Javascript 如何将此js代码传输到PHP
有人能帮我把我在下面写的javascript脚本转换成PHP吗?如有任何帮助,将不胜感激:Javascript 如何将此js代码传输到PHP,javascript,php,json,Javascript,Php,Json,有人能帮我把我在下面写的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;
<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>
$.getJSON('inc/data.php',{format:'json},函数(数据){
var valC=“”;
$.each(数据['canales'],函数(键,val){
var-str=key;
str=str.replace(“”,“-”);
valC=valC+'';
});
$(“#lista”).html(valC);
});
此php文件从数组生成一个json
字符串:
我想这就是你想要做的。请参见我的说明,您必须根据自己的能力和要求取消对两个选项之一的注释:
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>
类getJSON
{
受保护的$valC;
受保护的$getFile;
公共函数构造($filename='inc/data.php')
{
$this->valC=“”;
//如果可以编辑第二页,请删除该页上的第65行并取消注释:
//$this->doOpt1($filename);
//如果您仍停留在第二页,请取消注释:
//$this->doOpt2($filename);
}
私有函数doOpt1($filename)
{
//包括页面
包括_一次($filename);
//只需分配数组
$this->getFile=$lista->canales;
}
私有函数doOpt2($filename)
{
//创建缓冲区
ob_start();
//包括回显json字符串的页面
包括($filename);
//捕获缓冲区
$data=ob_get_contents();
//冲洗
ob_end_clean();
//解码json
$this->getFile=json_decode(trim($data),true);
}
公共函数createLinks()
{
if(空($this->getFile)| |!is_数组($this->getFile))
返回$this->valC;
foreach($this->getFile as$key=>$val){
$this->valC.='
';
}
返回$this->valC;
}
}
$makeLinks=new getJSON();
?>
编辑 经过进一步的讨论,听起来你想从原文中删减。看看这是否是你想要做的。它需要一个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);
}
}
我想这就是你想要做的。请参见我的说明,您必须根据自己的能力和要求取消对两个选项之一的注释:
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>
类getJSON
{
受保护的$valC;
受保护的$getFile;
公共函数构造($filename='inc/data.php')
{
$this->valC=“”;
//如果可以编辑第二页,请删除该页上的第65行并取消注释:
//$this->doOpt1($filename);
//如果您仍停留在第二页,请取消注释:
//$this->doOpt2($filename);
}
私有函数doOpt1($filename)
{
//包括页面
包括_一次($filename);
//只需分配数组
$this->getFile=$lista->canales;
}
私有函数doOpt2($filename)
{
//创建缓冲区
ob_start();
//包括回显json字符串的页面
包括($filename);
//捕获缓冲区
$data=ob_get_contents();
//冲洗
ob_end_clean();
//解码json
$this->getFile=json_decode(trim($data),true);
}
公共函数createLinks()
{
if(空($this->getFile)| |!is_数组($this->getFile))
返回$this->valC;
foreach($this->getFile as$key=>$val){
$this->valC.='
';
}
返回$this->valC;
}
}
$makeLinks=new getJSON();
?>
编辑
经过进一步的讨论,听起来你想从原文中删减。看看这是否是你想要做的。它需要一个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);
}
}
你能编辑第2页吗?或者你被卡住了吗?你想做什么?第2页,如果它没有破坏你网站其他地方的其他内容,你只需要删除将数组转换为json的脚本的最后几行,然后回声itI正在尝试获取所有缩略图,这个url“”在我的网页中创建一个目录,我只带了嵌入代码。我想你是想把它放在post StackOverflow中,而不是PasteBina中。你能编辑第2页吗?你想做什么?第2页,如果它没有破坏你网站其他地方的其他内容,您只需删除将数组转换为json的脚本的最后几行,然后回显itI。我正在尝试获取所有缩略图,这个url“”以在我的网页中创建一个目录,我只在其中获取嵌入代码。我想您的意思是将其放在post StackOverflow中,而不是pastebinSee中,如果上述编辑有效的话。它将从他们的页面中刮取数据。非常感谢您对它的帮助,我只有一个问题,它可能会得到例如:我的意思是所有的div内容,不仅你可能能够通过类或其他方式获得div?:看看上面的编辑是否有效。它将从他们的页面中刮取数据。非常感谢您对它的帮助,我只有一个问题,它可能会得到例如:我的意思是,所有的div内容,不仅你可能能够得到div b