1

以下に貼り付けたコードは、私の PC では動作しますが、ホスティング (PHP 5.2.13 がインストールされている) では動作しません。

$source = file_get_contents('http://example.com/example', 0);
$dom = new DOMDocument;
@$dom->loadHTML($source);
$dom->preserveWhiteSpace = false;

$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="item"]');

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n<root>\r\n";

foreach($tags as $tag)
    $xml .= "\t<tag>" . trim($tag->nodeValue) . "</tag>\r\n";

$xml .= '</root>';

$xml_file = 'tags.xml';
$fopen_handle = fopen($xml_file, 'w+');
fwrite($fopen_handle, $xml);
fclose($fopen_handle);

私のホスティングでは、foreach ループは実行されません。つまり、XML でこれだけを取得します。

<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>

前もって感謝します。

4

1 に答える 1

0

あなたのホスティングは file_get_contents をサポートしていません (allow_url_fopen は iirc の設定です)

これを試して:

$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch); 
于 2010-10-11T12:16:35.227 に答える