且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php cURL登录到jsp网站并返回HTML

更新时间:2023-09-10 23:18:58

以下是您的情况的几个建议...

Here are a few suggestions for your situation...


  • 为简单起见使用相同的curl句柄

    这减少了为每个请求复制选项的需要。在开始时设置大多数选项,并且只做一次。我主要参考cookie选项,用户代理,跟随位置等。

    然后,您可以为每个单独的请求设置URL和请求方法。

    您甚至可以获得通过向请求添加 Keep-Alive 标头来提高额外的性能,所以如果远程服务器支持它,那么将使用相同的连接来进行多个请求,而不必每次都重新连接。

  • Re-use the same curl handle for simplicity
    This reduces the need to duplicate options for each request. Set the majority of your options at the beginning and do it only once. I refer mostly to cookie options, user-agent, follow-location etc.
    You can then set the URL and request method for each individual request you make.
    You can even gain additional performance by adding a Keep-Alive header to your request so if the remote server supports it, the same connection will be used to make multiple requests without having to reconnect each time.

CURLOPT_FOLLOWLOCATION 设为 true 并从头开始

尝试按照您所看到的浏览器所做的操作。也就是说,请求web根;如果网站将您重定向到安全检查网址,cURL将跟踪该重定向并捕获进程中设置的任何Cookie。如果发送重定向,一个cURL请求可能会导致多个HTTP请求。然后继续填写登录表单。

Set CURLOPT_FOLLOWLOCATION to true and start from the beginning
Try to follow exactly what you see the browser do. That is, request the web root; if the site redirects you to the security check URL, cURL will follow that redirect and capture any cookies set in the process. One cURL request can result in multiple HTTP requests if a redirect is sent. Then proceed to "fill out" the login form.

使用 http_build_query对于您的帖子数据

设置帖子字符串的方式没有问题,但数据必须进行网址编码。使用数组使用 http_build_query()更容易操作,并且将生成可以直接提供给cURL的网址编码字符串。

Use http_build_query() for your post data
There is nothing wrong with the way you set up your post string, but the data must be url-encoded. Using http_build_query() with an array is easier to manipulate and will result in an url-encoded string you can feed directly to cURL.

另请参见此回答我几天前发布了一个试图做类似的人。我还发布了一些参考其他一些答案,包含使用cURL请求多个URL的完整示例;只是看看这些答案应该帮助你了解如何做你想要的。特别请参阅此答案这是我在文章中提到的第一个参考,因为它显示如何通过发出几个帖子请求和最终获取请求登录到Google。

See also this answer I posted a couple of days ago for a person trying to do something similar. I also posted a few references to some other answers that contain full samples of requesting multiple URLs using cURL; just looking at those answers should help you get an idea of how to do what you want. Especially see this answer which was the first reference in the post I mentioned as it shows how to log into Google by making several post requests and finally a get request.