卷曲多url可以';t执行


Curl Multi url can't execute

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

我编写了以下代码来使用curl执行多url。

首先在数组中存储url。

为了执行,我使用foreach。

但我不知道为什么不能从url中获取数据?

$urls = array(
"http://emalls.ir/%D9%84%DB%8C%D8%B3%D8%AA-%D9%82%DB%8C%D9%85%D8%AA_%D9%84%D9%86%D8%B2-%D8%AF%D9%88%D8%B1%D8%A8%DB%8C%D9%86-%D9%81%DB%8C%D9%84%D9%85-%D8%A8%D8%B1%D8%AF%D8%A7%D8%B1%DB%8C~Category~643",
"http://emalls.ir/%D9%84%DB%8C%D8%B3%D8%AA-%D9%82%DB%8C%D9%85%D8%AA_%D9%84%D9%86%D8%B2-%D8%AF%D9%88%D8%B1%D8%A8%DB%8C%D9%86-%D9%81%DB%8C%D9%84%D9%85-%D8%A8%D8%B1%D8%AF%D8%A7%D8%B1%DB%8C~Category~643",
 );
    $browsers = array(
   "standard" => array (
    "user_agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13",
    "language" => "en-us,en;q=0.5"
    ),
 );
 foreach ($urls as $url) {
echo "URL: $url'n";
foreach ($browsers as $test_name => $browser) {
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, $url);
    // set browser specific headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "User-Agent: {$browser['user_agent']}",
            "Accept-Language: {$browser['language']}"
        ));
    // we don't want the page contents
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    // we need the HTTP Header returned
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // return the results instead of outputting it
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    $page = curl_exec($ch);
    $dom = new DOMDocument('1.0', 'utf-8');
    libxml_use_internal_errors(true);
    @$dom->loadHTML(mb_convert_encoding($page, 'HTML-ENTITIES', 'UTF-8'));
    libxml_clear_errors();
    $xpath = new DOMXpath($dom);

    $data = array();
 $table_rows = $xpath->query("//table[@id='grdprice']/tr"); // target the   row (the browser rendered <tbody>, but actually it really doesnt have one)
   if($table_rows->length <= 0) { // exit if not found
echo 'no table rows found';
exit;
 }
 foreach($table_rows as $tr) { // foreach row
  $row = $tr->childNodes;
  if($row->item(0)->tagName != 'th') { // avoid headers
    $data[] = array(

                     'title'  =>  trim($row->item(0)->nodeValue),
                     'sensor' =>  trim($row->item(2)->nodeValue),

      );
  }
 }
}
echo'<pre>';
print_r($data);
 }

最终:

网址:

http://emalls.ir/%D9%84%DB%8C%D8%B3%D8%AA-%D9%82%DB%8C%D9%85%D8%AA_%D9%84%D9%86%D8%B2-%D8%AF%D9%88%D8%B1%D8%A8%DB%86-%D9%81%DB%8C%D9%84%D9%85%D9%85%D8%A8%D8%B1%D8%D8%A7%D8%B1%DB%8C~类别~643

未找到任何表行

好的,问题是您正在执行HEAD请求。这就是这行的作用:curl_setopt($ch, CURLOPT_NOBODY, 1);(基本上删除了正文)。此外,稍后在该文件中,您还可以调用curl_setopt($ch, CURLOPT_HEADER, 1);,它将返回服务器返回的Headers。查看您的代码,看起来您实际上只想从您请求的URI中获取XML,而不是头。因此,去掉这两行,您应该得到您期望的

的XML