Jump to content

php script to login and navigate a web page?


thirtywest

Recommended Posts

Greetings, 

New to PHP

I have a site at work that I log into to find the PDF that has the next month's schedules.  

I want to set a cron job that monitors the page for when schedules.pdf is uploaded.
The cron job part is easy.

 


Obstacles:

1. username and password field at login;
2. the page has a window (frame?) in it with three tabs you can hover and click. that produces the list of PDF files.  
3. While I would love to capture the PDF and email it I would settle for just getting an alert that the file is there for viewing. 


A. I'm comfortable scanning a page for particular text and sending a email/sms to let me know it was found.
B.  What I don't know how to do is "click" those spots to make the content appear as well as recognize the username / password fields to enter my credentials.

I started reading about CURL but I'm not sure if this is appropriate. 


Any guidance/direction would be helpful.

Link to comment
Share on other sites

There are a few different ways to do this, but all of them have a significant learning curve.  You really need to understand HTTP, and all the basics of HTML structure, forms, downloads etc.

The first path would be to use a Functional testing tool.  One long standing open source tool I can point you to would be Selenium, and in particular Selenium IDE.  What you do with this tool is record your interaction using their browser extension tool, and then use that script to perform your job afterwards.  I'm not a QA automation expert, but this is the basic idea behind how companies put together testing for their websites.  I included a short introduction video to give you an idea.  There are some additional details you would need, that are probably OS dependent, as to how you would schedule and invoke the selenium test.  PHP has tooling that will let you run selenium tests via PHP scripting.

The 2nd path would be to use an HTTP client library.  Basically, you write PHP code that simulates a web client session for you.  Again, these tools are often used for QA automated testing, but there are also many other use cases, like serverside integration with outside API's.  Curl as you mentioned, is sometimes an enabling technology in these client libraries, and you certainly could write curl code directly using the curl library, but the client libraries do tend to be easier to work with, and offer classes that hide complexity.

There are numerous client libraries you could look at but I'll provide 3 for your consideration:

  1. Symfony HTTP Client
  2. Guzzle 7
  3. Buzz

I'm a big fan of the Symfony components, as is a lot of the PHP developer community.  

Guzzle is probably the most used Client library, and has excellent documentaion.

Buzz is perhaps a little more terse and simpler.  It was created by a well known symfony contributor.  I pointed you to the github project.  To get a feel for it, navigate to the docs folder in github, and there you will find the documentation.

 

Some videos that might help:

 

Guzzle walk through based on an article.  This guy wrote an article, and on his windows machine walks through the article and demonstrates the things in the article. There is no actual voice over narrative, so it's just watching what he's doing, but that might be of help to you especially if you are on windows.  He uses VSCode which is a good choice if you're new to modern PHP development.

 

 

Gary Clarke has a lot of really good PHP development videos.  He walks through using the Symfony HTTP client in this series.  The one thing you have to understand is that he is a proponent of TDD (Test Driven Development) where you write unit tests first, and then develop your code to meet the criteria of the tests.  It's a really interesting way of developing code that has a lot of proponents, but there are more moving parts to it.  I still think it's an interesting watch that you might learn some things from:

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.