-3

..................................................................................

I write follwing code to execute multi url with curl.

first store urls in the arrays.

and the to execute I use foreach.

but I don't know why cant get data from 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 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);
 }

final:

URL:

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

no table rows found

rahavard
  • 57
  • 9
  • 2
    Have you debugged your code? Where does it fail? What is the expected output? What is the actual output? Have you enabled error reporting? What error messages do you get? On what specific line does it fail? – PeeHaa Apr 01 '15 at 10:22

1 Answers1

1

Ok, the problem is that you're doing a HEAD request. This is what this line does: curl_setopt($ch, CURLOPT_NOBODY, 1); (basically removes the body). Also, later on in the file you also call curl_setopt($ch, CURLOPT_HEADER, 1);, which returns the Headers that the server returns. Looking at your code, it looks like you actually want to just get the XML from the URI that you're requesting, not the headers. So remove those two lines and you should get the XML that you're expecting

Nathan
  • 331
  • 2
  • 4