php - Curl Multi url can't execute -
this question has answer here:
- does curl nobody fetch body? 2 answers
..................................................................................
i write follwing code execute multi url curl.
first store urls in arrays.
and execute use foreach.
but don't know why cant data urls?
$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 $url) { echo "url: $url\n"; foreach ($browsers $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']}" )); // don't want page contents curl_setopt($ch, curlopt_nobody, 1); // need http header returned curl_setopt($ch, curlopt_header, 1); // return results instead of outputting 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 row (the browser rendered <tbody>, doesnt have one) if($table_rows->length <= 0) { // exit if not found echo 'no table rows found'; exit; } foreach($table_rows $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); }
final:
url:
no table rows found
ok, problem you're doing head request. line does: curl_setopt($ch, curlopt_nobody, 1);
(basically removes body). also, later on in file call curl_setopt($ch, curlopt_header, 1);
, returns headers server returns. looking @ code, looks want xml uri you're requesting, not headers. remove 2 lines , should xml you're expecting
Comments
Post a Comment