我正在尝试编写一个简单的 PHP 脚本,它会自动设置新的 etherpad(请参阅 http://etherpad.com/).

他们还没有用于创建新垫的 API,所以我想知道是否可以用另一种方式做事。

经过一些尝试后,我发现如果您将一个随机字符串附加到 etherpad.com 到尚未创建的 pad,它会返回一个表单,询问您是否要在该地址创建一个新的 etherpad。如果您提交该表单,则会在该 URL 处创建一个新的便笺簿。

我当时的想法是,我可以使用 CURL 创建一个 PHP 脚本,该脚本将复制该表单并欺骗 etherpad 在我提供的任何 URL 上创建一个新的 pad。我写了脚本,但到目前为止我还无法让它工作。有人可以告诉我我做错了什么吗?

首先,这是 etherpad 创建页面上的 HTML 表单:

`

<p><tt id="padurl">http://etherpad.com/lsdjfsljfa-fdj-lsdf</tt></p>

<br/>
<p>There is no EtherPad document here. Would you like to create one?</p>

<input type="hidden" value="lsdjfsljfa-fdj-lsdf" name="padId"/>
<input type="submit" value="Create Pad" id="createPad"/>

`

然后这是我的代码,尝试使用 CURL 提交表单

$ch = curl_init();

//set POST variables
$url = "http://etherpad.com/ep/pad/create?padId=ldjfal-djfa-ldkfjal";
$fields = array(
  'padId'=>urlencode("ldjfal-djfa-ldkfjal"),
);

$useragent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";  

// set user agent  
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value; }
print_r($fields_string);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);
print_r($result);
//close connection
curl_close($ch);

当我运行脚本时,PHP 报告一切都正确执行,但 etherpad 没有创建我的 pad。有什么线索吗?

有帮助吗?

解决方案 3

这里的答案一个朋友帮我想出了:

  

他们显然做了一些饼干   验证,这就是为什么你的脚本   不工作。你可以发现这一点   通过加载新垫创建提示   页面,清除Cookie,然后   重新加载页面。它不会工作。   棘手,但有效最休闲   机器人。

     

下面是围绕得到一个脚本   局限性。只需插入您的期望   $ padId和远离你去。

<?php

$padId = 'asdfjklsjfgadslkjflskj';

$ch = curl_init();

# for debugging
curl_setopt($ch, CURLOPT_HEADER, true);

# parse cookies and follow all redirects
curl_setopt($ch, CURLOPT_COOKIEFILE, '/dev/null');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

# first, post to get a cookie
curl_setopt($ch, CURLOPT_URL, 'http://etherpad.com/' . urlencode 
($padId));
$result = curl_exec($ch);
echo $result;

# next, post to actually create the etherpad
curl_setopt($ch, CURLOPT_URL, 'http://etherpad.com/ep/pad/create');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'padId=' . urlencode($padId));
$result = curl_exec($ch);
echo $result;

curl_close($ch);

其他提示

我还没有研究这个特定的网站,但我想有它缺少一些重要的标头。这是一个非常普遍的做法,适用于几乎任何网站:

使用网络嗅探器(例如Wireshark)来捕获所有connectons。然后,发送POST字段与你的比较

这是更简单的方法是使用Netcat的。刚刚页面保存到磁盘,改变形式,网址的http://本地主机:3333 / 和运行

$ nc -l -p 3333

现在打开本地HTML文件,并在适当的字段填写。马上你会看到,会被传输到主机的所有标头。

(也有Mozilla Firefox的扩展,但一般来说,他们只是减慢浏览器没有提供多少好处。)

也读什么我已经张贴在要自动使用PHP卷曲因为它可能会帮助你实现在PHP中填充文本区域。

顺便说一句,你要发送的参数 “padId” 通过GET的的POST。这是没有必要的。检查什么EtherPad的形式实际使用,并坚持下去。

我的猜测是,你错过了饼干和/或引荐。它可以检查引用者,以确保人们没有无需确认创建垫。

Wireshark的帮助,但添加到您的卷曲,看看它是否工作。

直接从 HTML 或 TEXT 创建文件

使用 setText 或 setHTML API 端点。 http://etherpad.org/doc/v1.5.0/#index_sethtml_padid_html

要轻松完成此操作,请使用 Etherpad PHP 客户端 https://github.com/TomNomNom/etherpad-lite-client

从文件发布

此功能由 Etherpad 插件提供。要启用它...

安装以太网垫 ep_post_data 通过输入插件 npm install ep_post_data 在您的 Etherpad 实例上。

在您的客户端计算机上 CLI 类型: curl -X POST -d @yourfile.here http://youretherpad/post

  1. 将 yourfile.here 替换为您的文件
  2. 将 url 替换为您想要使用的 Etherpad 实例。

来源: http://blog.etherpad.org/2014/12/17/post-to-etherpad-with-this-simple-plugin/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top