我想设置诸如post变量名称和值之类的标题,并发送和期望响应。这也是一个安全问题,假设我想发送一个表单变量action="delete"和userid=100,比如说,我找到了一个接受ajax请求的文件。
curl是您的朋友!:)
假设您注意到example.org/process.php
处有一个端点,表单正在发布到该端点。您可以使用curl
从命令行轻松地定制自己的自定义请求。
$ curl -X POST --data "action=delete&userid=100" example.org/process.php
--data
或-D
标志允许您像传递HTML表单一样传递任意POST数据。您也可以同样轻松地设置HTTP请求标头:
$ curl --header "User-Agent: Mosaic" example.org/process.php
您可以确切地看到-v
(表示详细)标志发生了什么。对于上面的第一个例子,输出是:
* About to connect() to example.org port 80 (#0)
* Trying 192.0.43.10... connected
* Connected to example.org (192.0.43.10) port 80 (#0)
> POST /process.php HTTP/1.1
> User-Agent: curl/7.21.6 (x86_64-apple-darwin10.5.0) libcurl/7.21.6 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.22
> Host: example.org
> Accept: */*
> Content-Length: 24
> Content-Type: application/x-www-form-urlencoded
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Location: http://www.iana.org/domains/example/
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
* Connection #0 to host example.org left intact
* Closing connection #0
如果你使用的是包括Mac OS X在内的*NIX操作系统,你可能已经有了curl
,只需打开一个shell。如果您使用Ruby,我强烈建议您使用该语言的一组绑定。大多数PHP安装都支持curl,尽管界面非常糟糕。这些文档位于php.net上。
您可以为此使用CURL库。查看更多信息
您可以发送数据POST/GET方法,上传文件,SSL支持,cookie支持,ftp支持和更多的
您可能还想看看Snoopy(http://sourceforge.net/projects/snoopy/)
它是一个PHP类,设计用于充当web浏览器,具有许多有用的功能,如模拟HTTP请求、操作表单数据等。