0

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);
}
User014019
  • 1,177
  • 7
  • 31
  • 63
  • Have you considered sending an http request using php's curl to execute the csv_import.php script? – Craig Sep 15 '16 at 03:24
  • I haven't tried yet. I need the shell script to run the PHP file (from different server), get the file that will be imported with the same server where the shell script stored and schedule it thru cron job – User014019 Sep 15 '16 at 03:28
  • 1
    your `bash` script is broken in several ways. `php` "blah.php"` works for local `php` files, not urls. you could download the php script and then run it in `php` like this: `curl "http://example.com/csv_import.php" | php`, except that nearly any http server would not let you download the php code itself, it would execute the php on the server and then send you the result. if what you want to do is to tell `example.com` to run `csv_import.php`, that's possible: `curl "http://example.com/csv_import.php"`. – webb Sep 15 '16 at 03:46
  • @webb Do you mean I need to move the php file in same server where the bash stored? What if, the PHP file is stored in the different server? And the bash script will run this PHP file (the bash script is stored in different server). Should I do like this curl "http://example.com/csv_import.php"? – User014019 Sep 15 '16 at 03:53
  • if you want the server that has the shell script (the `bash` server) to do the mysql query and save the csv, then you do need to copy the php script to that server. but maybe you want the bash server to request the csv from the http server, then the http server runs the php and creates the csv, and gives the csv to the bash server, yes? if so, use `curl "http://example.com/csv_import.php" > my.csv` on the bash server, and modify the php to send back a csv, e.g., https://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php – webb Sep 15 '16 at 04:03
  • Are you simply trying to execute the csv_import.php file from a client server? OR are you trying to pass data to the client server and execute csv_import.php? – Craig Sep 15 '16 at 04:10
  • @Craig I'm trying to execute the csv_import.php from a client server while the shell script is from another server – User014019 Sep 15 '16 at 05:28
  • Ok - the answer below should work. Have you tried it? Be sure the csv_import.php file is in /htdocs so that it's accessible by the http request and that the csv_import.php file permissions allow it to be executed. – Craig Sep 15 '16 at 05:33
  • @webb, here's the scenario. In the server where the shell script stored. The csv file is also there. Now, the PHP where stored from another server will call the csv file. i tried your suggestion, but I got this `Could not open input` – User014019 Sep 15 '16 at 05:42

1 Answers1

0

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

Craig
  • 446
  • 5
  • 14
  • this is another php file? where i will stored this? – User014019 Sep 15 '16 at 03:55
  • just call it runCurl.php and store it in /bin and have cron execute 'php runCurl.php'...or store it in a directory of your choice and have cron call the full path, e.g. store in /home and cron executes 'php /home/runCurl.php' – Craig Sep 15 '16 at 03:59
  • how I call that PHP in shell script? – User014019 Sep 15 '16 at 05:57
  • http://stackoverflow.com/questions/5549562/running-php-script-php-function-in-linux-bash – Craig Sep 15 '16 at 06:05
  • I posted a new question.. http://stackoverflow.com/questions/39505241/how-to-upload-a-file-from-another-server-to-another-server-using-curl – User014019 Sep 15 '16 at 07:24