使用 PHP 和 CURL 创建新的 etherpad

发布于 2024-08-10 19:21:58 字数 1779 浏览 7 评论 0原文

我正在尝试编写一个简单的 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。有什么线索吗?

I'm trying to write a simple PHP script which automatically sets up new etherpads (see http://etherpad.com/).

They don't have an API (yet) for creating new pads so I'm trying to figure if I can do things another way.

After playing around some, I found that if you append a random string to etherpad.com to a not-yet-created pad, it'll come back with a form asking if you want to create a new etherpad at that address. If you submit that form, a new pad will be created at that URL.

My thought then was I could just create a PHP script using CURL that would duplicate that form and trick etherpad into creating a new pad at whatever URL I give it. I wrote the script but so far I can't get it working. Can someone tell me what I'm doing wrong?

First, here's the HTML form on the etherpad creation page:

`

<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"/>

`

Then here's my code which tries to submit the form using 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);

When I run the script, PHP reports back that everything executed correctly but etherpad doesn't create my pad. Any clues what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸伞微斜 2024-08-17 19:21:58

我还没有调查过这个特定的网站,但我猜想缺少一些重要的标题。这是一种非常通用的方法,几乎​​适用于任何网站:

使用 Wireshark 等网络嗅探器来捕获所有连接。然后将发送的 POST 字段与您的进行比较。

更简单的方法是使用 Netcat。只需将页面保存到磁盘,将表单 URL 更改为 http://localhost:3333/ 并运行

$ nc -l -p 3333

Now open本地 HTML 文件并适当填写字段。您将立即看到所有已传输到主机的标头。

(Mozilla Firefox 也有扩展,但一般来说,它们只会减慢浏览器速度,而没有提供太多好处。)

另请阅读我在 使用 php curl 自动填充文本区域,因为它可能会帮助您在 PHP 中实现。

顺便说一句,您将通过 GET POST 发送参数“padId”。那是没有必要的。检查 Etherpad 形式实际使用的内容并坚持使用。

I have not investigated this specific site but I guess there are some important headers which are missing. Here is a very general approach that is applicable for nearly any website:

Use a network sniffer such as Wireshark to capture all connectons. Then compare the sent POST fields with yours.

An even easier way is to use Netcat. Just save the page to disk, change the form-URL to http://localhost:3333/ and run

$ nc -l -p 3333

Now open the local HTML file and fill in the fields appropriately. Immediately you will see all headers that would have been transmitted to the host.

(There are also extensions for Mozilla Firefox but in general they just slow down the browser without providing much benefit.)

Also read what I have posted on To auto fill a text area using php curl as it might help you with your realization in PHP.

By the way, you are sending the parameter "padId" via GET and POST. That is not necessary. Check what the Etherpad-form actually uses and stick with it.

逆流 2024-08-17 19:21:58

我的猜测是您缺少 cookie 和/或引荐来源网址。它可能会检查推荐人,以确保人们不会在未经确认的情况下创建垫子。

Wireshark 会有所帮助,但将其添加到您的卷曲中,看看它是否有效。

My guess is that you're missing the cookies and/or the referrer. It may be checking the referrer to ensure people aren't creating pads without confirmation.

Wireshark will help, but add that to your curl and see if it works.

多像笑话 2024-08-17 19:21:58

这是一位朋友帮我想出的答案:

他们显然正在做一些饼干
验证,这就是你的脚本的原因
不工作。你可以找到这个
通过加载新的焊盘创建提示
页面,清除您的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);

Here's the answer a friend helped me come up with:

They're apparently doing some cookie
validation, that's why your script
isn't working. You can find this out
by loading the new pad creation prompt
page, clearing your cookies, and then
reloading the page. It won't work.
Tricky, but effective for most casual
bots.

Here's a script that gets around the
limitation. Just insert your desired
$padId and away you go.

<?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);
凉风有信 2024-08-17 19:21:58

直接从 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< /a>

从文件发布

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

通过在 Etherpad 实例上输入 npm install ep_post_data 来安装 Etherpad ep_post_data 插件。

在您的客户端计算机上 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/

To create a file directly from HTML or TEXT

Use the setText or setHTML API endpoint. http://etherpad.org/doc/v1.5.0/#index_sethtml_padid_html

To easily do this use the Etherpad PHP Client https://github.com/TomNomNom/etherpad-lite-client

To post from a file

This feature is provided by an Etherpad plugin. To enable it...

Install the Etherpad ep_post_data plugin by typing npm install ep_post_data on your Etherpad instance.

At your client machine CLI type: curl -X POST -d @yourfile.here http://youretherpad/post

  1. Replace yourfile.here with your file
  2. Replace the url with the Etherpad instance you want to work to.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文