Why does cURL return an empty string?

前端 未结 4 632
面向向阳花
面向向阳花 2020-12-20 11:36

I\'m having a problem with PHP\'s cURL returning an empty string with some URL\'s. I\'m trying to parse the OG metadata of different webpages and it works with all website

相关标签:
4条回答
  • 2020-12-20 11:54

    (That other answer is me also)

    This is what did it for me. It was looking for SSL verificaiton, which I happened to not need in this specific case.

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    0 讨论(0)
  • 2020-12-20 12:09

    These 5 lines did the magic for me.

       curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
       curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($ch, CURLOPT_VERBOSE, 1);
    
    0 讨论(0)
  • 2020-12-20 12:10

    My guess is that a site like the New York times has protection against such behavior. Most likely this is based on the user agent, which you can fake as so:

    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
    

    This is the most common agent btw.

    0 讨论(0)
  • 2020-12-20 12:11

    This is what did it for me. It was looking for SSL verificaiton, which I happened to not need in this specific case.

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    0 讨论(0)
提交回复
热议问题