0

次のスクリプトを使用して特定の Web サイトから値を取得しようとしていますが、これは有効な DOM ドキュメントではないと思います。別の方法があるかどうかを知りたいですか?

       <?php
      $curl_handle=curl_init();
      curl_setopt($curl_handle,CURLOPT_URL,'http://www.indiagoldrate.com/gold-rate-in-mumbai-today.htm');
      curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
      curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
      $buffer = curl_exec($curl_handle);
      curl_close($curl_handle);
      if (empty($buffer))
      {
          print "Sorry, example.com are a bunch of poopy-heads.<p>";
      }
      else
      {
          print $buffer;
      }
      ?>
4

2 に答える 2

0

これを試しfile_get_contentsてみましたか、

$str= htmlentities(file_get_contents('http://www.indiagoldrate.com/gold-rate-in-mumbai-today.htm'));

ファイル取得内容の読み取り

于 2013-07-26T04:49:58.790 に答える
0

コード内のページ ( http://www.indiagoldrate.com/gold-rate-in-mumbai-today.htm ) は有効な DOM ドキュメントではありませんが、それでも PHP のDOMDocumentで解析できます。たとえば、今日のムンベイ市の 1 グラム 22k ゴールドの価格を取得します。

libxml_use_internal_errors(true); //get rid of the warnings

$dom = new DOMDocument;
$dom->loadHTML($buffer);
$xp = new DOMXPath($dom);
$price = $xp->query('//*[@id="right_center"]/table[1]/tr[3]/td[2]/table/tr[1]/td[2]')->item(0)->nodeValue;

libxml_clear_errors();
libxml_use_internal_errors(false);

var_dump($price);
于 2013-07-26T07:34:17.263 に答える