How do I get text from a website using PHP?

后端 未结 7 2044
北海茫月
北海茫月 2020-12-31 11:33

So, I\'m working on a PHP script, and part of it needs to be able to query a website, then get text from it.

First off, I need to be able to query a certain website

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 11:41

    You can use file_get_contents or if you need a little more control (i.e. to submit POST requests, to set the user agent string, ...) you may want to look at cURL.

    file_get_contents Example:

    $content = file_get_contents('http://www.example.org');
    

    Basic cURL Example:

    $ch = curl_init('http://www.example.org');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3');
    
    $content = curl_exec($ch);
    
    curl_close($ch);
    

提交回复
热议问题