douhan6738 2012-03-07 21:06
浏览 127
已采纳

file_get_contents和变量

I have a text file filled with a list of IDs. Using PHP, I am trying to load a url for each of the IDs and pull something from that page (another id)

For example, if I have the IDs 555, 888, 222 I want to load the URLs

http://example.edu/bvl.P_Sel?facultyID=555

http://example.edu/bvl.P_Sel?facultyID=888

http://example.edu/bvl.P_Sel?facultyID=222

I tried to get the content via
file_get_contents("http://example.edu/bvl.P_Sel?facultyID=$lines[0]");

where $lines is an array of the IDs. This returns the following error:

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 

That url is an example, but the url in the error does work when I visit it manually. And if I replace the file_get_contents variable with an actual ID, like ?facultyID=222, it works perfectly.

I visited this question's answer How to post data in PHP using file_get_contents? and tried assigning a variable in the $postdata array to a variable, and I get the same exact error only with ?facultyID=XXX removed from the error message's url.

My implementation of the latter is here.

  • 写回答

3条回答 默认 最新

  • duanjizi9443 2012-03-07 21:25
    关注

    Those encoded characters when you use the urlencode function (%0D%0A) are a new line, so maybe you array of id's have them in each element. Try this:

    // your code to generate the lines array
    file_get_contents("http://example.edu/bvl.P_Sel?facultyID=" . trim($lines[0]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端