Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 under my control. Then i think there, they are blocking the incoming cURL requests because when i test with the normal HTML <form>, it is working. But not via cURL anyway.

So i think they have done some restriction to cURL.

Then my questions here are:

  1. Can a Server restrict/block the cURL incoming requests?
  2. If so, can i trick/change the HTTP Header (User Agent) in my initiating cURL script?
  3. Or is there any other possible stories?

Thanks!

like image 542
夏期劇場 Avatar asked Jul 23 '13 03:07

夏期劇場


People also ask

How do I pass the user agent in curl?

You can use the -A or --user-agent command-line option to pass your own User-Agent string to Curl. By default, Curl sends its own User-Agent string to the server in the following format: "curl/version. number".

How do I stop curl requests on my server?

Servers cannot block cURL requests per se, but they can block any request that they do not like. If the server checks for some parameters that your cURL request does not satisfy, it could decide to respond differently.

What is Curlopt_useragent?

CURLOPT_USERAGENT. It's the CURLOPT_USERAGENT line which sets the user agent string. Specify whatever you want as the value and that's what will appear in the weblogs of the server the request is going to.


2 Answers

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.

like image 96
Black0CodeR Avatar answered Oct 27 '22 21:10

Black0CodeR


  $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';   $curl=curl_init();   curl_setopt($curl, CURLOPT_USERAGENT, $agent); 
like image 40
srain Avatar answered Oct 27 '22 21:10

srain