Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在我的页面上修改yahoo weather外观?_Php_Jquery_Html_Yahoo Weather Api - Fatal编程技术网

Php 如何在我的页面上修改yahoo weather外观?

Php 如何在我的页面上修改yahoo weather外观?,php,jquery,html,yahoo-weather-api,Php,Jquery,Html,Yahoo Weather Api,我刚刚在我的页面上找到了基本的yahoo weather提要 以下代码呈现以下图像: $ipaddress = $_SERVER['REMOTE_ADDR']; // API username and key masked as sensitive and irrelevant to this question $locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddres

我刚刚在我的页面上找到了基本的yahoo weather提要

以下代码呈现以下图像:

$ipaddress = $_SERVER['REMOTE_ADDR'];

// API username and key masked as sensitive and irrelevant to this question
$locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddress."&format=xml";

$xml = simplexml_load_file($locationstr);

$city = $xml->city;

// We'll only be accounting for certain cities to start off with...
switch ($city)
{
    case "Pretoria":
        $loccode = "SFXX0044";

        $weatherfeed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
        if (!$weatherfeed) die("weather check failed, check feed URL");
        $weather = simplexml_load_string($weatherfeed);

        readWeather($loccode);
        break;
}

function readWeather($loccode)
{
    $doc = new DOMDocument();
    $doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");

    $channel = $doc->getElementsByTagName("channel");

    foreach($channel as $ch)
    {
        $item = $ch->getElementsByTagName("item");

        foreach($item as $rcvd)
        {
            $desc = $rcvd->getElementsByTagName("description");

            // Save the weather data to a session variable for placement on the page
            $_SESSION["weather"] = $desc->item(0)->nodeValue;
        }
    }
}
在我看来,它看起来相当糟糕,所以我想改变这里的整体设计,使之与我网站的其他部分相适应

我有一个想法,就是用jquery重写呈现的html,但我没有任何进展

以下是我当前正在渲染的代码:

<div id="weather-feed">
    <img src="http://l.yimg.com/a/i/us/we/52/34.gif"/>
    <b>Current Conditions:</b>
    Partly Cloudy, 17 C
    <b>Forecast:</b>
    Tue - PM Thunderstorms. High: 26 Low: 16
    Wed - Mostly Sunny. High: 27 Low: 16

    <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Pretoria__SF/*http://weather.yahoo.com/forecast/SFXX0044_c.html">Full Forecast at Yahoo! Weather</a>
    (provided by <a href="http://www.weather.com" >The Weather Channel</a>)
</div>
基本上,我要做的就是修改这个渲染代码,如下所示:

<div id="weather-feed">
    <div class="weather-icon">
        <img src="http://l.yimg.com/a/i/us/we/52/34.gif"/>
    </div>
    <div class="conditions">
        Partly Cloudy, 17 C
    </div>
    <div class="forecast">
        <b>Forecast:</b>
        Tue - PM Thunderstorms. High: 26 Low: 16
        Wed - Mostly Sunny. High: 27 Low: 16

        <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Pretoria__SF/*http://weather.yahoo.com/forecast/SFXX0044_c.html">Full Forecast at Yahoo! Weather</a></div>
    <div class="credit">
        provided by <a href="http://www.weather.com" >The Weather Channel</a>
    </div>
</div>
我找不到任何能指导我如何做这件事的东西

我是否可以利用任何api调用来获得请求数据的不同表示形式


如果没有,谁能建议我如何成功地重写这段代码?也许有一个小例子…

获取您得到的结果html块,然后使用jquery修改它的垃圾。幸运的是,主div有一个很好的id。您应该能够从主html块中获取单个块,如

将为您提供img的src。玩游戏,使用html并重建它

继续:假设您知道或能够了解jquery如何工作以及document.ready是什么。
Jquery将是您在这里的朋友。我张贴的例子打击。看看这个。到处都有线索

这是我的想法,但我在执行上失败了,你有没有机会再详细解释一下?container div id=weather提要是我在页面中编码的提要。原始代码看起来像是在进步,我正在进步:我在图像周围有div,但我现在有点迷茫,不知道如何处理周围没有标记的情况和预测区域好吧,我认为这已经奏效了,但现在我如何将每个部分的内容写入div?我在页面上看到的是到处都是[object object],如果你看看我的例子,你可以在用你收到的东西创建你自己的html时创建你自己的元素。[object object]表示返回给您的是对象类型。请看,您是否有幸弄明白了这一点: