file_get_contents script works with some websites but not others

前端 未结 3 1421
轻奢々
轻奢々 2021-01-01 04:24

I\'m looking to build a PHP script that parses HTML for particular tags. I\'ve been using this code block, adapted from this tutorial:



        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 05:09

    It just requires a user-agent ("any" really, any string suffices):

    file_get_contents("http://www.freshdirect.com",false,stream_context_create(
        array("http" => array("user_agent" => "any"))
    ));
    

    See more options.

    Of course, you can set user_agent in your ini:

     ini_set("user_agent","any");
     echo file_get_contents("http://www.freshdirect.com");
    

    ... but I prefer to be explicit for the next programmer working on it.

提交回复
热议问题