如何将数据从Greasemonkey发送到PHP(WAMP)服务器? [英] How to send data from Greasemonkey to a PHP (WAMP) server?

查看:91
本文介绍了如何将数据从Greasemonkey发送到PHP(WAMP)服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Greasemonkey脚本,在其中计算六个变量(时间,移动,滚动,sav,prin,book和url).

I am creating a Greasemonkey script where I am calculating six variables (time, move, scroll, sav, prin, book and url).

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

I need to send these variables' data to my PHP page so that these could be inserted in a MySQL table using a WAMP server.

请,任何人都可以给我确切的代码,因为我是这一切的新手?

Please, can anyone give the exact code to it as I am new to all this?

我的Greasemonkey脚本是:

My Greasemonkey script is:

{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的一面是:


and php side is:

$jsonData   = json_decode($HTTP_RAW_POST_DATA);

echo jsonData.u;


此代码未运行.另外,我尝试检查是否已使用jsonData.u传递了变量u,但它仅显示"jsonData.u".


this code is not running.. Plus I try to check if my variable u has been passed using jsonData.u, but it just echoes "jsonData.u".

推荐答案

我只是创建用户脚本,以使Greasemonkey从打开的网页中抓取数据并向本地XAMPP服务器上托管的PHP脚本发布POST参数,从而可以运行本地Python脚本来实现自动化有效.

I just create userscript to make Greasemonkey scrape data from webpage opened and POST parameters to the PHP script hosted on my local XAMPP server which could run local Python scripts to automate works.

这也是从Javascript呈现的页面中获取数据的一种很酷的方法,这对于Python抓取工具来说很难,甚至比Selenium更好:P

This also a cool method for scrping data from Javascript rendered pages, which is hard for Python scraper, even better than Selenium :P

此PHP示例网址中的参数由&分隔:

The parameters are seperated by & in this PHP sample url:

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测试脚本:

Python test script:

#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")

希望它可以提供帮助!

这篇关于如何将数据从Greasemonkey发送到PHP(WAMP)服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆