1

Im thinking in a cron which executes daily one php script. This script will make a file_get_contents() to one url I assign. Can i do this for simulate a user's visit? Does it work like a visit?

$page = file_get_contents('http://www.example.com/');
echo $page;
Daniel Garcia Sanchez
  • 2,306
  • 5
  • 21
  • 35
  • Not sure what do you count as a visit. It should count as a request made to the host server, other than that, it depends on what they count as a visit. – Boian Ivanov Jan 23 '18 at 11:57
  • for example, going to google analytics...do you think it will count as a visitor? i think yes, have to try it! :) thanks for answer – Daniel Garcia Sanchez Jan 23 '18 at 11:58
  • I don't think so, google analytics counts when the js is activated. File_get_contents, as well as curl make requests to the server and return the code to you. They do not execute the code. You might be able to "cheat" it though, check this blog post : https://blog.evantahler.com/curl-your-way-into-the-google-analytics-api-9abbd5ea5a2a – Boian Ivanov Jan 23 '18 at 12:00

2 Answers2

1

You can "simulate" this kind of action, but it's better to be done with curl. Also to do this I would recommend going through this stackoverflow post, it explains all the variables which are needed to be supplied by doing a server side request, rather than opening the page through a browser and loading the analytics js.

Boian Ivanov
  • 164
  • 1
  • 3
  • 14
1

If your visit is counted by Google Analytics implemented through javascript, then no, it won't work - file_get_contents() doesn't run any javascript, just downloads the file. However, you could do it by sending page view through PHP: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

Note that javascript gathers more information about the user than PHP can, so use with caution.

If your intention is to check whether the script was run, there are better and easier ways, such as logging any opening, or from specific IP, to your database or a text file.

Everything I suggested here presumes you can edit the opened file - your http://www.example.com/index.php. If you need to trigger Google Analytics on a site you can't edit, you need something way more robust, like a web crawler or scraper, to execute the javascript. For inspiration: http://www.jacobward.co.uk/using-php-to-scrape-javascript-jquery-json-websites/

LuH
  • 443
  • 5
  • 11