doulianxi0587
2016-09-19 22:56
浏览 78

幻影将php变量传递给js

I know there are hundreds of questions about how to pass php variable to JS but i tried a several solution and nothing worked.

I'm pretty new to javascript, i'm using phantomjs to have the URL of an external webpage.

I have this script.js

var page = require('webpage').create();
page.onError = function(msg, trace) {
  //prevent js errors from showing in page.content
  return;
};
page.open('http://www.google.com', function () {
    console.log(page.content); //page source
    phantom.exit();
});

called by getweb.php

<?php
exec ('phantomjs script2.js ', $html);
var_dump($html) ;
?>

works perfect. But If i want to put a php variable in place of google.com it does not work, i guess (just guess...) it's because phantom is no interpreted by navigator.

I tried to put

    <script>
var adress = '<?php echo "http://www.google.com" ; ?>'; 
</script>

before to execute and change my script with

var page = require('webpage').create();
page.onError = function(msg, trace) {
  //prevent js errors from showing in page.content
  return;
};
page.open(adress, function () {
    console.log(page.content); //page source
    phantom.exit();
});

but it just take hours without nothing happening.

What can I do to pass a php variable to script.js ?

thanks a lot

图片转代码服务由CSDN问答提供 功能建议

我知道有关于如何将php变量传递给JS的数百个问题但是我尝试了几个解决方案并且没有任何效果 。

我是javascript的新手,我正在使用phantomjs来获取外部网页的URL。

我有这个脚本。 js

  var page = require('webpage')。create(); 
page.onError = function(msg,trace){
 //阻止显示js错误 在page.content 
 return; 
}; 
page.open('http://www.google.com',function(){
 console.log(page.content); //页面源
  phantom.exit(); 
}); 
   
 
 

由getweb.php调用

 &lt;?  php 
exec('phantomjs script2.js',$ html); 
var_dump($ html); 
?&gt; 
   
 
 

工作正常。 但如果 我想把一个php变量放在google.com上它不起作用,我猜(只是猜猜......)这是因为幻影不是由导航器解释的。

我试过 把

 &lt; script&gt; 
var adress ='&  LT;?php echo“http://www.google.com”;  ?&GT;';  
&lt; / script&gt; 
   
 
 

在执行和更改我的脚本之前

  var page = require(  'webpage')。create(); 
page.onError = function(msg,trace){
 //阻止js错误显示在page.content 
 return; 
}; 
page.open(地址,功能)  (){
 console.log(page.content); //页面源
 phantom.exit(); 
}); 
   
 
 

但它 只花几个小时没有任何事情发生。

如何将php变量传递给script.js?

非常感谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufeikuang7724 2016-09-19 23:05
    已采纳

    I think the best thing to do is pass in extra arguments when you execute Phantom.JS with your script. I can't test in your context, but try something like this. In your PHP:

    exec ('phantomjs script2.js "http://example.com/"', $html);
    

    Then in your JavaScript, do a console.log() on system.args. One of them should be http://example.com/. See also: http://phantomjs.org/api/system/property/args.html

    点赞 打赏 评论

相关推荐 更多相似问题