简体   繁体   中英

Scraping data from url in php, first url is working but not others which have same structure only id is different

I scrape data from the following urls:

-e.g:http://www.example.com/?api=de&id=100
-e.g:http://www.example.com/?api=de&id=101
-e.g:http://www.example.com/?api=de&id=102
-e.g:http://www.example.com/?api=de&id=103
  1. I can access all url directly and see all the data is there in Json format.
  2. I tried without simple_html_dom.
  3. I tried with simple_html_dom (With file_get_contents,file_get_html).

By using above method i can only see the data of "id=100" but when I change the id to 101 or 102 etc. it does not work. All the urls have the same structure, only the id is different.

I tried simple_html_dom (With file_get_contents , file_get_html ) Also without `simple_html_dom, however in both cases I get the same result as empty except for the first url.

   //First Method
   $url = "http://www.example.com/?api=de&id=101";
   $json = file_get_contents($url);
   $MyData = json_decode($json, true);
   print_r($MyData); // Giving Empty Array (i.e.array())

   //Second Method
   $fileget = new simple_html_dom();
   $url = "http://www.example.com/?api=de&id=101";
   $json = file_get_html($url);
   print_r($MyData); // Giving Empty Array (i.e.array())

But when I set id=100 then all is working well. ( Here the output is Correct )

   // Now with id=100
   $url = "http://www.example.com/?api=de&id=100";
   $json = file_get_contents($url);
   $MyData = json_decode($json, true);
   print_r($MyData); 
   //Output is
   [{"name":"A","class":"1"},{"name":"B","class":"2"},{"name":"C","class":"3"}]

I don't get why, when the urls all have the same structure, only first one works and not the others.

Try to put a try catch block and print the exception variable. Also try printing $json value . You may be able track the issue cause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM