如何在php中使用curl从外部网站获取div

时间:2013-08-28 05:04:22

标签: php html curl

我正在寻找卷曲代码,我可以从其他网页上获取外部网站的div。

例如:这是外部网站:http://www.uberstrike.com/public_profile/2585774

我想从类“user_info”中提取数据。

我在curl中看到了很多代码,但没有一个可以工作。

2 个答案:

答案 0 :(得分:2)

他们有一个API。

示例代码(用下面的选项替换url):

$json = json_decode(file_get_contents('http://www.uberstrike.com/profile/all_stats/2585774'), true);
print_r($json);

分数:http://www.uberstrike.com/profile/all_stats/2585774

Array
(
    [headshots_record] => 146
    [nutshots_record] => 91
    [damage_dealt_record] => 27060
    [damage_received_record] => 32695
    ... truncated ...
)

用户信息:http://www.uberstrike.com/profile/user_info/2585774

Array
(
    [name] => deadbodies
    [level] => 80
    [xp] => 5587757
    [clan_name] => E-PIPE
    [clan_tag] => EPIPE
    [accesslevel] => 0
    [creation_date] => 2011-08-17T06:40:07Z
)

其他内容:http://www.uberstrike.com/profile/user_loadout/2585774

Array
(
    [weapon_1] => 1243
    [weapon_2] => 1656
    [weapon_3] => 1370
    [melee_weapon] => 0
    [holo] => 1716
    [upper_body] => 1644
    [lower_body] => 1567
    ... truncated ...
)

答案 1 :(得分:1)

试试这个:

//CURL
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'WEBSITE_URL');//enter your url here
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch); //get the page contents
preg_match($s_searchFor, $file_contents, $matches); //match the element
$file_contents = $matches[0]; //set the file_contents var to the matched elements