Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set browser language when using curl to retrieve contents?

Tags:

browser

php

curl

It am using curl in php to get contents from external website. I am getting contents from websites like https://www.flickr.com/. The site will show different content (in different languages) for browsers in different languages. I want to get English content only. I have tried:

  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");

But it doesn't work. It retrieve Chinese contents instead. Any advice?

like image 749
user2335065 Avatar asked Jun 09 '14 06:06

user2335065


People also ask

How do I get my browser to request a curl?

From Chrome and Edge On the line of the specific resource you are interested in, you right-click with the mouse and you select "Copy as cURL" and it will generate a command line for you in your clipboard. Paste that in a shell to get a curl command line that makes the transfer.

How do you pass a Useragent 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 you curl a Web page?

The syntax for the curl command is as follows: curl [options] [URL...] In its simplest form, when invoked without any option, curl displays the specified resource to the standard output. The command will print the source code of the example.com homepage in your terminal window.

How do you use multiple headers in curl command?

To pass multiple headers in a curl request you simply add additional -H or --header to your curl command. For standard HTTP header fields such as User-Agent, Cookie, Host, there is actually another way to setting them.


1 Answers

Set the Accept-Language request header using CURLOPT_HTTPHEADER like so:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Language: en']);
like image 114
Ja͢ck Avatar answered Oct 26 '22 07:10

Ja͢ck