xml - php domdocument How to read a document using a url
伊谢尔伦
伊谢尔伦 2017-05-16 13:12:41
0
1
551

Because of project needs, I need to use php to modify the value on xml. Use DOMdocument to do it.
The code is as follows

 $xmlpatch ='http://www.*****.com/vr/wtest/vtour/tour.xml';
        $xml = simplexml_load_file($url);
        $doc = new DOMDocument();
        $doc -> formatOutput = true;
        if($doc -> load($xmlpatch)) {
        $root = $doc -> documentElement;
        $elm = $root -> getElementsByTagName('scene');
        $hotspot = $elm[1] ->getElementsByTagName('hotspot');
        $hotspot[0] ->setAttribute('videourl',$videourl1);
        $hotspot[1] ->setAttribute('onclick',$videourl2);
    
    
        $doc->save($xmlpatch);
        } 

The code should be fine. The problem is that I originally wrote a relative path in $xmlpatch. Because I used the local environment package, there is no problem.
But the environment php files and xml on the server are placed across domains, so I changed them to absolute paths. But I found that the script failed. Someone online suggested using simplexml to do it. But I can indeed read it after experimenting, but I can't find a function to modify attributes on w3cschool.
Please help me~ I would be very grateful~ Can you tell me how to modify the attributes of simplexml, or how to read the domdocument using the url.

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
迷茫

First use curl to read the information of the xml file, and then use DOMdocument or simplexml to process it.

<?php

$url = 'http://www.abc.com/sitemap.xml';

$header = array(
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
    'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding: gzip, deflate',
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
curl_close($curl);

if ($xml = simplexml_load_string($data)) {
    $xml->row->name = 'new_name'; // Edit XML Object node
    echo $xml->asXML();
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template