0

I am learning to POST data to a URL. I want to send some data to the URL everyday automatically. I've read this curl-less and this curl, both seem to be a little complicated for me - that I need to take care of something other than the data itself.

I am thinking maybe I can just use a fake form, hardcode the data into the form and after page loaded, programmatically "click" to submit the form, which should be much easier for me.

Is this doable? Is there any downside? Among the 3 ways, when should I use which one?

Community
  • 1
  • 1
shenkwen
  • 2,994
  • 5
  • 31
  • 65

1 Answers1

1

Q: Can cron job open a php page or execute some php code?

A: Yes, you can do nearly everything with cron job

Use cron jobs with curl.

Sample curl POST request

1- Prepare your script

myscript.php

// set post fields
$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];

$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
var_dump($response);

2- Set your cron

Run your script automatically with cron jobs on your linux environment like this

Community
  • 1
  • 1
hakiko
  • 4,737
  • 6
  • 49
  • 96