How to get the contents of the site via php?

<?php Header("Content-Type: text/html;charset=UTF-8"); include 'simple_html_dom.php'; $ch = curl_init(); $curl_log = fopen("curl_log.txt", 'w+'); curl_setopt($ch, CURLOPT_STDERR, $curl_log); curl_setopt($ch, CURLOPT_VERBOSE, TRUE); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'); curl_setopt($ch, CURLOPT_URL, "http://anistar.ru/new/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=iconv('CP1251', 'UTF-8', curl_exec($ch)); exit('Ошибка curl: ' . curl_error($ch)); curl_close($ch); echo $result; echo $curl_log; ?> 

This code does not work. blank screen is white. file_get is the same. How to be? is there any possibility?

New Logs

 * About to connect() to anistar.ru port 80 (#0) * Trying 185.11.145.7... * connected > GET /new/ HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 Host: anistar.ru Accept: */* < HTTP/1.1 200 OK < Server: nginx < Date: Sun, 06 Sep 2015 10:41:58 GMT < Content-Type: text/html < Transfer-Encoding: chunked < Connection: keep-alive < Keep-Alive: timeout=20 < Vary: Accept-Encoding < X-XSS-Protection: 1; mode=block < * Connection #0 to host anistar.ru left intact * Closing connection #0 
  • exit('Ошибка curl: ' . curl_error($ch)); before curl_close($ch); put it. - Visman
  • Put. Now the curl Error is displayed: - Andro
  • Since only the Ошибка curl: issued, the error is not in curl. Remove the command from the code. - Visman
  • @Visman, at the top I posted the logs - Andro
  • You better look at the browser in the code of your blank page;) It really does not come empty, but with js code. Probably the site is worth checking just against parsing. - Visman

1 answer 1

 <?php $ch = curl_init(); ... echo htmlspecialchars($result); 

Suddenly the screen is not white :

enter image description here


You should not guess at the tea leaves when working with Curl in PHP and waste time.

You can enable logging in the file and see what happens:

 $curl_log = fopen("curl_log.txt", 'w+'); curl_setopt($ch, CURLOPT_STDERR, $curl_log); curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 

Or use, in my opinion, a visually more visual way when debugging on a local server. You can install Fiddler, or another Web Debugging Proxy (for example, Charles) and enable the proxy in the surl options:

 curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888"); 

Then all requests will be in full view.

  • Updated ......... - Andro