How to call a PHP from another server using shell script? I have a PHP file and a shell script, the 2 files are stored in different server.
I have this Shell script:
#!/bin/bash
php "http://example.com/csv_import.php"
But when I run this command manually, I got an error: Could not open input
PHP:
if (($handle = fopen($directory_root."filename.csv", "r")) !== FALSE){
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
$sql = "INSERT INTO tablename(col1,col2,col3,col4,col5)
VALUES ('".mysql_escape_string($data[0])."',
'".mysql_escape_string($data[1])."',
'".mysql_escape_string($data[2])."',
'".mysql_escape_string($data[3])."',
'".mysql_escape_string($data[4])."')";
$query = mysql_query($sql);
}
fclose($handle);
}
On your calling server, have your cron job execute the following script (that also resides on your calling server):
<?php
$options = array(
CURLOPT_URL => 'http://example.com/csv_import.php',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
?>
Thus, on client:
cron calls runCurl.php
runCurl.php issues http request to server's csv_import.php file
On server:
csv_import.php executes, writing data to your db (that resides on same server as csv_import.php)
csv_import.php is stored in /htdocs so that it's accessible by http request
csv_import.php file permissions are set so that it is executable