我有一个填充了ID列表的文本文件。 使用PHP,我正在尝试为每个ID加载一个URL并从该页面中提取一些内容(另一个id)
例如,如果我有ID 555,888,222 我想加载网址
http://example.edu/bvl.P_Sel?facultyID=555
http://example.edu/bvl.P_Sel?facultyID=888
http://example.edu/bvl.P_Sel?facultyID=222
我试图通过
获取内容
file_get_contents("http://example.edu/bvl.P_Sel?facultyID=$lines[0]");
其中$ lines是ID的数组。 这将返回以下错误:
Warning: file_get_contents(http://example.edu/bvl.P_Sel?facultyID=222) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404
该url是一个示例,但是当我手动访问它时,错误中的url确实有效。如果我将file_get_contents变量替换为实际ID,例如?facultyID=222
,它就可以完美运行。
我访问了这个问题的答案How to post data in PHP using file_get_contents?,并尝试将$ postdata数组中的变量分配给变量,只有从错误消息的url中删除?facultyID=XXX
才能得到同样的错误。
我对后者的实施是here。
答案 0 :(得分:2)
当您使用urlencode函数(%0D%0A)时,那些编码字符是一个新行,所以也许您的id数组在每个元素中都有它们。试试这个:
// your code to generate the lines array
file_get_contents("http://example.edu/bvl.P_Sel?facultyID=" . trim($lines[0]));
答案 1 :(得分:1)
$lines = array(813667,1124279,760643,668461,2868,33613);
print_r($lines);
输出:
Array ( [0] => 813667 [1] => 1124279 [2] => 760643 [3] => 668461 [4] => 2868 [5] => 33613 )
这样:
foreach($lines as $key => $value):
echo '<pre>';
print_r($lines[$key]);
endforeach;
输出:
813667
1124279
760643
668461
2868
33613
并且: $ get = file_get_contents(“http://example.edu/bvl.P_Sel?facultyID=$lines[0]”); 的print_r($获得);
输出:
Example Domains
As described in RFC 2606,
we maintain a number of domains such as EXAMPLE.COM and EXAMPLE.ORG
for documentation purposes. These domains may be used as illustrative
examples in documents without prior coordination with us. They are
not available for registration.
有什么问题? :)这是你需要的吗?
答案 2 :(得分:0)
尝试使用CURL进行抓取,并发布数据,因为它更强大,更先进。