2013-01-06 125 views
0

我想從玩家的蒸汽配置文件中獲取圖片,並在我的網站上使用它們,因此當他們更改圖片/照片時,它會改變我的位置。從url獲取圖片

這只是一個會加載一次,加載時它應該得到最新的網址。 你可以使用這個URL進行測試,我需要的是在這個類的div中獲取圖像url:並且只複製圖片url。

鏈接測試steamprofile:http://steamcommunity.com/profiles/76561197991341238

碼我曾嘗試:

foreach($html->find('div[class=avatarFull]') as $div) 
{  
    foreach($div->find('img') as $img) 
    { 
     echo "<img src='" . $img->src . "'/>";   
    } 
} 

回答

-2

我覺得正則表達式preg_match)更簡單的方法來做到這一點

$html = file_get_contents("http://steamcommunity.com/profiles/76561197991341238"); 
preg_match('/<div class="avatarFull"><img src="(.*)" \/><\/div>/i', $html, $profile_picture); // $profile_picture[1]; is the url itself 

,如果你想抓住從同一個URL多張照片然後替換preg_matchpreg_match_all$profile_picture[1]

+0

$ html和$ profilepicture我從哪裏得到thoose值,就像我評論「Kirugan」我是一個大的noob當時.. :) –

+0

$ html是頁面內容,他們抓住使用捲曲,'file_get_contents'或任何URL解析器... $ profile_picture是結果,它不應該是一個主動變量e,所以當你使用url時,只需使用echo $ profile_picture [1]; ..回答更新 – Osa

+1

不要使用正則表達式來解析HTML。請參閱http://stackoverflow.com/q/3577641/1592648 –

3

使用steam web apiGetPlayerSummaries方法它的工作原理沒有HTML報廢

+0

它它,因爲我編碼去年很長一段時間,所以我可以得到一個示例代碼,這就是爲什麼我給了一個鏈接:)因爲我學習allover一次:) Thx –

0

運行一個循環使用koraktor的Steam Condenser,您可以輕鬆地將這些信息並使用它。

require_once('steam/db.php'); 
$_STEAMAPI = "YOURAPIKEYHERE"; // You get this from http://steamcommunity.com/dev/apikey 
$playersStr = "";    // This is a comma separated list of profile IDs 
           // if you don't have those, Steam Condenser has functions to acquire it 
$players = array();    // For this example, I'm building an array of players assuming you'll have multiple players returned. 
           // You are free to skip this if you are only pulling one back 
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$playersStr"; 
$json_object= file_get_contents($url); 
$json_decoded = json_decode($json_object); 

foreach ($json_decoded->response->players as $player) 
{ 
    $players[$player->steamid]['personaname'] = $player->personaname;  // In game name 
    $players[$player->steamid]['profileurl'] = $player->profileurl;  // URL to their Steam Community Page 
    $players[$player->steamid]['avatar'] = $player->avatar;    // Small Avatar Icon URL 
    $players[$player->steamid]['avatarmedium'] = $player->avatarmedium; // Thumbnail avatar URL 
    $players[$player->steamid]['avatarfull'] = $player->avatarfull;  // Full sized Avatar URL 
}  

如果你沒有一個球員的個人資料的ID,但確實有蒸汽ID,就可以得到這樣的配置文件ID:

$profile_id = SteamId::convertSteamIdToCommunityId($steam_id);