【问题标题】:cURL without cURL- problem没有 cURL 的 cURL - 问题
【发布时间】:2010-01-01 05:12:06
【问题描述】:

我想从远程服务器复制文件,但似乎远程主机正在使用会话,并且需要 cookie。

我用了这个方法 定义变量后...

$url="http://example.com/stamp/stamp.jsp?tp=&arnumber=5255176&isnumber=5255174";
$nn = "\r\n";
$cookies="";
$request = GET . " " . str_replace ( " ", "%20", $url ) . " HTTP/1.1" . $nn . "Host: " . $host . $nn . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" . $nn . "Accept: */*" . $nn . "Accept-Language: en-us;q=0.7,en;q=0.3" . $nn . "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" . $nn . "Pragma: no-cache" . $nn . "Cache-Control: no-chache" . $nn  . $proxyauthorization . $referer . $cookies . "Connection: Close";

$fp= socksopen($Proxy,$port, $errno, $errstr, 15 );

socket_set_timeout ( $fp, 120 );

fputs ( $fp, $request );

fflush ( $fp );

//read header

$i = 1;

do {

$header.= @fgets ( $fp, 128 );

$i++;

} while ( strpos ( $header, $nn . $nn ) === false );

echo $header;

fclose ($fp);

现在我有了带有 url 所说的 cookie 的标题:

HTTP/1.1 302 Moved Temporarily Content-length: 0 Content-type: text/html Server: Sun-ONE-Web-Server/6.1 Date: Mon, 28 Dec 2009 13:40:53 GMT Set-cookie: ERIGHTS=5YAaxxmNsMuTK87E1TCAohwDRuyqBaCgM-oehmg24bkzHplCtmgn7zMA==;path=/;domain=.example.org Set-cookie: WLSESSION=1528980108.20480.0000; expires=Tue, 29-Dec-2009 13:40:52 GMT; path=/ Location: http://example.com/stamp/stamp.jsp?tp=&arnumber=5255176&isnumber=5255174"&tag=1 Via: 1.1 proxy-server1 Proxy-agent: Sun-Java-System-Web-Proxy-Server/4.

然后我做了一些刺痛代码并在代码中构建了这个:

$cookies="cookie: ERIGHTS=5YAaxxmNsMuTK87E1TCAohwDRuyqBaCgM-oehmg24bkzHplCtmgn7zMA==,WLSESSION=1528980108.20480.0000";

使用上述相同的方法重新请求 url 我又得到了 与另一个 cookie 相同的标题

似乎远程网站每次都将我视为首次访问者并设置新的 cookie 增益

有一件事,我要复制的文件正是一个 .pdf 文件

我用浏览器查了一下,结果:

说网址是:

http://example.com/stamp/stamp.jsp?tp=&arnumber=5344171&isnumber=5344169

我检查了 URL 的源代码,它包含重定向到另一个页面,该页面要么是带有 iframe 的 javascript

页面来源(来自我的浏览器)

     <frameset rows="65,35%">
<frame src="http://example.com/stamp/banner.jsp" frameborder="0" framespacing="0" framepadding="0" scrolling="no" />
<frame src="http://example.com/stampPDF/getPDF.jsp?tp=&arnumber=5255176&isnumber=5255174" frameborder="0" />

如您所见,当我单击 URL 时,它会将我重定向到 URL2 URL2 是这样的:http://example.com/stampPDF/getPDF.jsp?tp=&amp;arnumber=5255176&amp;isnumber=5255174

URL2 包含我可以轻松复制(下载)的 .pdf 文件。

但是当我使用上面提到的代码时,$url="http://example.com/stamp/stamp.jsp?tp=&amp;arnumber=5255176&amp;isnumber=5255174" 它需要根据我的请求设置 cookie,当接收到 cookie 并根据我的请求设置和发送 cookie 时,它​​会再次回复一个新的 cookie !!

你们怎么了?

【问题讨论】:

  • 为什么要重新实现一个 HTTP 库?卷曲有问题吗?它具有用于传递 cookie、跟踪重定向等的命令行选项...
  • cURL 不能与代理一起正常工作,我尝试了 5 个代理,但只有一个可以与 cURL 一起工作。我不知道为什么,所以我构建了自己的代码。

标签: php curl


【解决方案1】:

也许你应该检查你的 cookie 组合逻辑。 cookie 标头中的名称-值对应该用 ';' 分隔不带“,”:http://msdn.microsoft.com/en-us/library/aa920098.aspx#wce50concookieheadersanchor2
而且(我相信你会这样做)我会使用 Fiddler、HttpAnalyzer 或其他类似工具仔细检查所有请求标头。

【讨论】:

    【解决方案2】:

    您是否将http://example.com/stampPDF/getPDF.jsp?tp=&arnumber=5255176&isnumber=5255174 作为推荐人?该网站可能正在检查。

    【讨论】:

    • 正如你所建议的,我包括了引用者---->相同的结果:(
    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多