doufu9947 2014-08-24 20:41
浏览 31
已采纳

PHP获取XML值,FOREACH创建用户和复制图像。 怎么了?

So i have this cron.php to do some tasks every 15minutes. Cron works fine, but something is wrong with my php code and i cant figure it out.

I want to copy the remote xml file to local folder (this works), then, parse thru the xml file and get all display_name and image_url values. Then, create local folder with username and copy its thumb there... how hard can this be? i've been at it for past 5 days :(

Can you help?

#!/usr/local/bin/php -f

<?php

// Copy remote xml to local dir

copy ('http://www.remoteweb.com/api/?format=xml', '/includes/cache/feed.xml');


// connects to xml
$xmlor = '/includes/cache/feed.xml';
$doc = New DOMDocument();
$doc->load($xmlor);
// gets xml values as vars
$resource = new SimpleXMLElement($xmlor, null, true);

foreach( $doc->getElementsByTagName('display_name') as $user ) {

// create dir with username
if (!file_exists('/includes/cache/_thumbs/'$user'')) {
    mkdir('/includes/cache/_thumbs/'$user'', 0755, true);
}

foreach( $doc->getElementsByTagName('image_url') as $thumb ) {
// copy user thumb to user folder
copy ($thumb, '/includes/cache/_thumbs/'$user'');
                        }
                }
?>
  • 写回答

2条回答 默认 最新

  • drxt70655 2014-08-24 23:28
    关注

    XML Example:

    <response>
    <resource>
    <display_name>Madsprings</display_name>
    <image_url>https://serverexample.com/avatar/madsprings.jpg</image_url>
    </resource>
    </response>
    

    So i changed the code to this,

    $xml = simplexml_load_file("/home/sgs/public_html/includes/cache/ctbfeed.xml");
    
    foreach($user = $xml->xpath("//response//resource//display_name") as $ctb_user){
    echo $ctb_user;
    
    if (!file_exists('/includes/cache/_thumbs/' . $ctb_user)) {
        mkdir('/includes/cache/_thumbs/' . $ctb_user, 0755, true);
            }
    
    foreach($thumb = $xml->xpath("//response//resource//image_url") as $thumb_url){
    $path = parse_url($thumb_url, PHP_URL_PATH);
    $local_thumb = basename($path);
    copy ($thumb_url, ('/includes/cache/_thumbs/' . $ctb_user . '/' . $local_thumb));
            }
    }
    

    And it appears to be working :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?