如何将数据从Greasemonkey发送到PHP(WAMP)服务器?

问题描述:

我正在创建一个Greasemonkey脚本,用于计算六个变量(时间,移动,滚动,sav,prin,book和url)。如何将数据从Greasemonkey发送到PHP(WAMP)服务器?

我需要将这些变量的数据发送到我的PHP页面,以便可以使用WAMP服务器将这些数据插入到MySQL表格中。

请问,任何人都可以提供确切的代码给它,因为我是新手吗?

我的Greasemonkey脚本是:

{var ajaxDataObj = { 
    s:  sav, 
    p:  prin, 
    b:  book, 
    t:  finalTime, 
    u:  url, 
    a:  totalScroll, 
    b:  tot 
}; 

var serializedData = JSON.stringify (ajaxDataObj); 

GM_xmlhttpRequest ({ 
    method: "POST", 
    url: "localhost/anuja/greasemonkey.php", 
    data: serializedData, 
    headers: { 
     "Content-Type": "application/json", 
     "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used. 
     "Accept": "text/xml"   // If not specified, browser defaults will be used. 
} } 


和PHP方面是:

$jsonData = json_decode($HTTP_RAW_POST_DATA); 

echo jsonData.u; 


这个代码不运行..另外,我尝试检查,如果我的变量u一直通过使用jsonData.u,但它只是回声“jsonData.u”。

+0

为什么你在greasemonkey中实现它,而不是在正常的javascipt(你把它放在你的wamp服务器上)呢? – wimh 2013-02-23 17:07:08

+0

@Wimmel,那将是因为GM脚本运行在他没有服务/控制的页面上。 – 2013-02-23 23:44:54

+1

请参阅http://stackoverflow.com/questions/9401009/greasemonkey-ajax-request-is-not-sending-data – 2013-02-24 02:52:12

我只是创建userscript,使Greasemonkey从网页打开和POST参数刮取数据到我的本地XAMPP服务器上托管的PHP脚本,该脚本可以运行本地Python脚本来自动化作品。

这也为从Javascript scrping数据阴凉方法生成的网页,这是很难的Python刮刀,比硒甚至更好:P

的参数由&在这个PHP示例网址分隔:

http://www.sciencedirect.com/search?qs=Vascular%20stent&authors=&pub=&volume=&issue=&page=&origin=home&zone=qSearch&offset=1200 

GM脚本部分:

// @grant  GM_xmlhttpRequest 
unsafeWindow.sendPhp2Py = function(){ 
    //var ytitle = 'Youtube - ' + document.querySelector('div.yt-user-info').textContent.trim(); 
    var yurl = document.location.href; 
    //console.info(ytitle); 
    //console.info(yurl); 
    var ret = GM_xmlhttpRequest({ 
     method: "POST", 
     url: "http://localhost/php_run_py.php", 
     //data: "ytitle="+ ytitle + "&yurl="+ yurl, 
     data: "yurl="+ yurl, 
     headers: { 
      "Content-Type": "application/x-www-form-urlencoded" 
     }, 
     onload: function(response) { 
      console.log(response); 
      // readyState 4 = complete 
      // status = 200 OK 
      if(response.readyState == 4 && response.status == 200){ 
       document.querySelector('#myPhpPyBtn').textContent = 'Sent to PHP!'; 
      } 
     }, 
     onerror: function(e){ 
      console.log(e); 
      document.querySelector('#myPhpPyBtn').textContent = 'PHP not connected'; 
     } 
    }); 
}; 

PHP素文字:

<?php 
    echo $_POST['yurl']; 
//传递多个参数如下可以行得通 参数里包含空格哦也可以!!赞 Multiple parameters pass 
    //echo shell_exec("python test.py \"".$_POST['ytitle']."\" \"".$_POST['yurl']."\""); 
    //echo shell_exec("python GreaseMonkey_Php_Youtube_srt_generator.py ".$_POST['yurl']); 
//更安全 Safer 
    //system(escapeshellcmd("python test.py \"".$_POST['ytitle']."\" \"".$_POST['yurl']."\"")); 
    system(escapeshellcmd("python GreaseMonkey_Php_Youtube_srt_generator.py ".$_POST['yurl'])); 
?> 

的Python测试脚本:

#coding=utf-8 
import sys 
f = open("test.txt", "a+") 
f.write(sys.argv[1] + "\n" + sys.argv[2]+ "\n") 
f.close() 
print ("some output") 

希望它可以帮助!