PHP cURL how to add the User Agent value OR overcome the Servers blocking cURL requests?

前端 未结 4 1050
独厮守ぢ
独厮守ぢ 2020-12-05 10:54

I am transferring an Object Array. I have a cURL client (submitter) on own Server and listening script on other\'s Server, which one is not

相关标签:
4条回答
  • 2020-12-05 11:32

    Server cannot block only cURL requests because they are just HTTP requests. So changing User Agent of your cURL can solve your problem, as server will think you are connecting through browser presented in UA.

    0 讨论(0)
  • 2020-12-05 11:37
    1. In the server side, we can block some requests by recognize the header fields(including refer, cookie, user-agent and so on) in http request, the ip address, access frequency. And in most case, requests generated by machine usually has something different than human requests,for example, no refer & cookie, or with higher access frequency, we can write some rules to deny these requests.

    2. According to 1, you can try your best to simulate real requests by filling the header fields, using random and slower frequency, using more ip addresses. (sounds like attack)

    3. Generally, using lower frequency and do not make heavy load for their server, follow their access rules, they will seldom block your requests.

    0 讨论(0)
  • 2020-12-05 11:40

    IF you are still facing the problem then do the following.

    1.

    $config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
    
    curl_setopt($curl, CURLOPT_USERAGENT, $config['useragent']);
    curl_setopt($curl, CURLOPT_REFERER, 'https://www.domain.com/');
    

    2.

    $dir                   = dirname(__FILE__);
    $config['cookie_file'] = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';
    
    curl_setopt($curl, CURLOPT_COOKIEFILE, $config['cookie_file']);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $config['cookie_file']);
    

    NOTE: You need a COOKIES folder in directory.

    3.

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    

    If doing these don't solve the problem then Give the Sample Input/Output/Error/etc. So, that more precise solution can be provided.

    0 讨论(0)
  • 2020-12-05 11:49
      $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
      $curl=curl_init();
      curl_setopt($curl, CURLOPT_USERAGENT, $agent);
    
    0 讨论(0)
提交回复
热议问题