Read in external links in PHP

情到浓时终转凉″ 提交于 2019-12-12 05:16:25

问题


I know how to do this in Ruby, but I want to do this in PHP. Grab a page and be able to parse stuff out of it.


回答1:


Take a look at cURL. Knowing about cURL and how to use it will help in many ways as it's not specific to PHP. If you want something specific however, you can use file_get_contents which is the recommended way in PHP to get the contents of a file into a string.




回答2:


$file = file_get_contents("http://google.com/");

How to parse it depends on what you are trying to do, but I'd recommend one of the XML libraries for PHP.




回答3:


You could use fopen in read mode: fopen($url, 'r'); or more simply file_get_contents($url);. You could also use readfile(), but file_get_contents() is potentially more efficient and therefore recommended.

Note: these are dependent on config (see the linked manual page) but will work on most setups.

For parsing, simplexml is enabled by default in PHP.

$xmlObject = simplexml_load_string($string);
// If the string was valid, you now have a fully functional xml object.

echo $xmlObject->username;



回答4:


Its funny, I had the opposite question when I started rails development



来源:https://stackoverflow.com/questions/1659183/read-in-external-links-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!