简体   繁体   中英

How to spoof useragent for a php script run through cron

I am running a PHP script through cron every 30 minutes which parses and save some pages of my site on the same server. I need to run the script as Firefox or chrome useragent, since the parsed pages has some interface dependency on CSS3 styles.

I tried this within my script:

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");

But the Firefox or Chrome dependent stylesheets doesn't load with it. I tried with both double and single quotes.

My question is: Is it possible to spoof useragent for scripts run through server and not browser and how.

NOTE: I know that my browser dependency for interface is bad. But I want to know if this is even possible.

EDIT

My script runs through the sitemap on the server and create a html cache of the pages in sitemap. It don't need to execute any js or css file. Only thing is to spoof useragent so that the cache generated contains the extra js and css files for that browser that are included in the header.

You can consider that I am generating cache files for all browser type - IE, webkits and firefox. So, that I can serve the cache file to the user based on their browser. At this time I am serving the same files to all users, that is without the extra css files.

I think I will need to hardcode the css file into my page so that it is always included in the cache (non-compatible browser won't show any change but it will only increase the file over-head for them). Thanks anyways

When you run a php script through Cron, the idea is that it is a script, not a webpage being requested. Even if you could spoof the useragent, the css and javascript isn't going to excecuted as if it would be running inside a real web browser. The point of cron is to run scripts, raw scripts, that do, for example, file operations.

Well, at first I would look at your user agent identification. I think it is unnecessary complicated, try simply Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) .

If this should not work for then you could try to execute the curl call as a shell command with exec() . In this case you could run into problems that the page is not really rendered itself. You could workaround this by using a X virtual framebuffer . This would make your page render in memory, not showing any screen output - ergo behave like a browser.

You could do it like this:

exec("xvfb-run curl [...]");

You can also set the user agent by using ini_set('user_agent', 'your-user-agent'); Maybe that will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM