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);
}
9
  • Have you considered sending an http request using php's curl to execute the csv_import.php script? Commented Sep 15, 2016 at 3: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 Commented Sep 15, 2016 at 3: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". Commented Sep 15, 2016 at 3: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 "example.com/csv_import.php"? Commented Sep 15, 2016 at 3: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., stackoverflow.com/questions/217424/… Commented Sep 15, 2016 at 4:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

this is another php file? where i will stored this?
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'
how I call that PHP in shell script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.