PHP exec ping doesn't properly work on Unix server - php

I want to ping a range of ips on the server that runs Unix
exec('ping -c 4 '.$ip, $output, $return_var);
Problem is that, it doesn't properly ping the ips that actually gets pinged but can't be found on ns_lookup in cmd. what I mean is that some of these ips gets pinged and some not. On localhost, that runs windows, it works fine.
this is the $output array I get when I ping that ip on server (runs Unix):
array(5) {
[0]=>
string(54) "PING 91.208.144.2 (91.208.144.2) 56(84) bytes of data."
[1]=>
string(0) ""
[2]=>
string(36) "--- 91.208.144.2 ping statistics ---"
[3]=>
string(64) "4 packets transmitted, 0 received, 100% packet loss, time 2999ms"
[4]=>
string(0) ""
}
that's the $output array I get when I ping it on localhost (on Windows):
array(11) {
[0]=>
string(0) ""
[1]=>
string(43) "Pinging 91.208.144.2 with 32 bytes of data:"
[2]=>
string(50) "Reply from 91.208.144.2: bytes=32 time=9ms TTL=248"
[3]=>
string(51) "Reply from 91.208.144.2: bytes=32 time=10ms TTL=248"
[4]=>
string(51) "Reply from 91.208.144.2: bytes=32 time=11ms TTL=248"
[5]=>
string(50) "Reply from 91.208.144.2: bytes=32 time=8ms TTL=248"
[6]=>
string(0) ""
[7]=>
string(33) "Ping statistics for 91.208.144.2:"
[8]=>
string(56) " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"
[9]=>
string(46) "Approximate round trip times in milli-seconds:"
[10]=>
string(48) " Minimum = 8ms, Maximum = 11ms, Average = 9ms"
}
exec and ping is available on server cuz it works perfect with the ips that gets pinged and can be looked up in cmd.
Any ideas ?

Related

Special character 0x85 (octal 205, decimal 133) is crashing PHP's SimpleXMLElement

I am trying to parse an XML string containing the special octal character 205. The XML string comes from a shoutcast server. It seems that this character crashes the internals of SimpleXMLElement.
// This is my metadata.php file:
<?php
header('Content-type: application/json');
$output = file_get_contents('error.xml.bak');
$xml = new SimpleXMLElement($output, LIBXML_NOERROR|LIBXML_ERR_NONE) or die('Something is wrong');
echo 'OK';
?>
I am getting the following error:
kazepis$ php metadata.php
PHP Fatal error: Uncaught Exception: String could not be parsed as XML in /var/www/html/wolfclub/metadata.php:4
Stack trace:
#0 /var/www/html/radio/metadata.php(4): SimpleXMLElement->__construct('<?xml version="...', 32)
#1 {main}
thrown in /var/www/html/radio/metadata.php on line 4
You can find the sample XML with the problematic character (decimal 133, octal 205) here:
https://wetransfer.com/downloads/f1b615c1cc09c8262cdd9965991b9cd420200123155505/801ab3
or inline:
<?xml version="1.0" standalone="yes" ?><!DOCTYPE SHOUTCASTSERVER [<!ELEMENT SHOUTCASTSERVER (CURRENTLISTENERS,PEAKLISTENERS,MAXLISTENERS,REPORTEDLISTENERS,AVERAGETIME,SERVERGENRE,SERVERURL,SERVERTITLE,SONGTITLE,SONGURL,IRC,ICQ,AIM,WEBHITS,STREAMHITS,STREAMSTATUS,BITRATE,CONTENT,VERSION,WEBDATA,LISTENERS,SONGHISTORY)><!ELEMENT CURRENTLISTENERS (#PCDATA)><!ELEMENT PEAKLISTENERS (#PCDATA)><!ELEMENT MAXLISTENERS (#PCDATA)><!ELEMENT REPORTEDLISTENERS (#PCDATA)><!ELEMENT AVERAGETIME (#PCDATA)><!ELEMENT SERVERGENRE (#PCDATA)><!ELEMENT SERVERURL (#PCDATA)><!ELEMENT SERVERTITLE (#PCDATA)><!ELEMENT SONGTITLE (#PCDATA)><!ELEMENT SONGURL (#PCDATA)><!ELEMENT IRC (#PCDATA)><!ELEMENT ICQ (#PCDATA)><!ELEMENT AIM (#PCDATA)><!ELEMENT WEBHITS (#PCDATA)><!ELEMENT STREAMHITS (#PCDATA)><!ELEMENT STREAMSTATUS (#PCDATA)><!ELEMENT BITRATE (#PCDATA)><!ELEMENT CONTENT (#PCDATA)><!ELEMENT VERSION (#PCDATA)><!ELEMENT WEBDATA (INDEX,LISTEN,PALM7,LOGIN,LOGINFAIL,PLAYED,COOKIE,ADMIN,UPDINFO,KICKSRC,KICKDST,UNBANDST,BANDST,VIEWBAN,UNRIPDST,RIPDST,VIEWRIP,VIEWXML,VIEWLOG,INVALID)><!ELEMENT INDEX (#PCDATA)><!ELEMENT LISTEN (#PCDATA)><!ELEMENT PALM7 (#PCDATA)><!ELEMENT LOGIN (#PCDATA)><!ELEMENT LOGINFAIL (#PCDATA)><!ELEMENT PLAYED (#PCDATA)><!ELEMENT COOKIE (#PCDATA)><!ELEMENT ADMIN (#PCDATA)><!ELEMENT UPDINFO (#PCDATA)><!ELEMENT KICKSRC (#PCDATA)><!ELEMENT KICKDST (#PCDATA)><!ELEMENT UNBANDST (#PCDATA)><!ELEMENT BANDST (#PCDATA)><!ELEMENT VIEWBAN (#PCDATA)><!ELEMENT UNRIPDST (#PCDATA)><!ELEMENT RIPDST (#PCDATA)><!ELEMENT VIEWRIP (#PCDATA)><!ELEMENT VIEWXML (#PCDATA)><!ELEMENT VIEWLOG (#PCDATA)><!ELEMENT INVALID (#PCDATA)><!ELEMENT LISTENERS (LISTENER*)><!ELEMENT LISTENER (HOSTNAME,USERAGENT,UNDERRUNS,CONNECTTIME, POINTER, UID)><!ELEMENT HOSTNAME (#PCDATA)><!ELEMENT USERAGENT (#PCDATA)><!ELEMENT UNDERRUNS (#PCDATA)><!ELEMENT CONNECTTIME (#PCDATA)><!ELEMENT POINTER (#PCDATA)><!ELEMENT UID (#PCDATA)><!ELEMENT SONGHISTORY (SONG*)><!ELEMENT SONG (PLAYEDAT, TITLE)><!ELEMENT PLAYEDAT (#PCDATA)><!ELEMENT TITLE (#PCDATA)>]><SHOUTCASTSERVER><CURRENTLISTENERS>1</CURRENTLISTENERS><PEAKLISTENERS>3</PEAKLISTENERS><MAXLISTENERS>5000</MAXLISTENERS><REPORTEDLISTENERS>1</REPORTEDLISTENERS><AVERAGETIME>1</AVERAGETIME><SERVERGENRE>public</SERVERGENRE><SERVERURL>http://www.virtualdj.com/</SERVERURL><SERVERTITLE>wolf</SERVERTITLE><SONGTITLE>BARRY WHITE - YOU'RE THE FIRST,THE LAST ▒ </SONGTITLE><SONGURL></SONGURL><IRC>wolf</IRC><ICQ>wolf</ICQ><AIM>wolf</AIM><WEBHITS>80</WEBHITS><STREAMHITS>6</STREAMHITS><STREAMSTATUS>1</STREAMSTATUS><BITRATE>96</BITRATE><CONTENT>audio/mpeg</CONTENT><VERSION>1.9.8</VERSION><WEBDATA><INDEX>0</INDEX><LISTEN>0</LISTEN><PALM7>6</PALM7><LOGIN>0</LOGIN><LOGINFAIL>0</LOGINFAIL><PLAYED>0</PLAYED><COOKIE>0</COOKIE><ADMIN>1</ADMIN><UPDINFO>1</UPDINFO><KICKSRC>0</KICKSRC><KICKDST>0</KICKDST><UNBANDST>0</UNBANDST><BANDST>0</BANDST><VIEWBAN>0</VIEWBAN><UNRIPDST>0</UNRIPDST><RIPDST>0</RIPDST><VIEWRIP>0</VIEWRIP><VIEWXML>69</VIEWXML><VIEWLOG>0</VIEWLOG><INVALID>3</INVALID></WEBDATA><LISTENERS><LISTENER><HOSTNAME>78.129.222.56</HOSTNAME><USERAGENT>curl/7.29.0</USERAGENT><UNDERRUNS>0</UNDERRUNS><CONNECTTIME>216</CONNECTTIME><POINTER>0</POINTER><UID>2</UID></LISTENER></LISTENERS><SONGHISTORY><SONG><PLAYEDAT>1579791561</PLAYEDAT><TITLE>BARRY WHITE - YOU'RE THE FIRST,THE LAST ▒ </TITLE></SONG></SONGHISTORY></SHOUTCASTSERVER>
Any ideas why this is happening?
My operating system:
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
Thank you!
PHP 7.3.11-1~deb10u1 (cli) (built: Oct 26 2019 14:14:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.11-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies
I found this:
https://www.php.net/manual/en/class.simplexmlelement.php#107869
However the $errors array as shown in the line $errors = libxml_get_errors(); is always empty in my case. So that snippet did not help. Moreover i also got the following warnings:
PHP Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !\nBytes: 0x85 0x20 0x20 0x20 in Entity, line: 1 in /var/www/html/radio/metadata.php on line 6
[Fri Jan 24 18:56:12.290661 2020] [php7:warn] [pid 17910] [client 130.xxx.xxx.xxx:xxxxx] PHP Warning: simplexml_import_dom(): Invalid Nodetype to import in /var/www/html/radio/metadata.php on line 12
Anyway, I managed to get over this messy situation by using utf8_encode() to encode my string before feeding it to the SimpleXMLElement constructor.
My resulting "test" php file is:
<?php
// header('Content-type: application/json');
$output = file_get_contents('error.xml.bak');
$output = utf8_encode($output);
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($output);
var_dump($output);
$errors = libxml_get_errors();
var_dump($errors);
$xml = simplexml_import_dom($doc);
// $xml = new SimpleXMLElement($output, LIBXML_NOERROR|LIBXML_ERR_NONE) or die('Something is wrong');
var_dump($xml);
?>
which results in the following printout without any errors or warning whatsoever...
string(3396) "]>13500011publichttp://www.virtualdj.com/wolfBARRY WHITE - YOU'RE THE FIRST,THE LAST wolfwolfwolf806196audio/mpeg1.9.800600001100000000690378.129.222.56curl/7.29.00216021579791561 " array(0) { } object(SimpleXMLElement)#2 (22) { ["CURRENTLISTENERS"]=> string(1) "1" ["PEAKLISTENERS"]=> string(1) "3" ["MAXLISTENERS"]=> string(4) "5000" ["REPORTEDLISTENERS"]=> string(1) "1" ["AVERAGETIME"]=> string(1) "1" ["SERVERGENRE"]=> string(6) "public" ["SERVERURL"]=> string(25) "http://www.virtualdj.com/" ["SERVERTITLE"]=> string(4) "wolf" ["SONGTITLE"]=> string(64) "BARRY WHITE - YOU'RE THE FIRST,THE LAST " ["SONGURL"]=> object(SimpleXMLElement)#3 (0) { } ["IRC"]=> string(4) "wolf" ["ICQ"]=> string(4) "wolf" ["AIM"]=> string(4) "wolf" ["WEBHITS"]=> string(2) "80" ["STREAMHITS"]=> string(1) "6" ["STREAMSTATUS"]=> string(1) "1" ["BITRATE"]=> string(2) "96" ["CONTENT"]=> string(10) "audio/mpeg" ["VERSION"]=> string(5) "1.9.8" ["WEBDATA"]=> object(SimpleXMLElement)#4 (20) { ["INDEX"]=> string(1) "0" ["LISTEN"]=> string(1) "0" ["PALM7"]=> string(1) "6" ["LOGIN"]=> string(1) "0" ["LOGINFAIL"]=> string(1) "0" ["PLAYED"]=> string(1) "0" ["COOKIE"]=> string(1) "0" ["ADMIN"]=> string(1) "1" ["UPDINFO"]=> string(1) "1" ["KICKSRC"]=> string(1) "0" ["KICKDST"]=> string(1) "0" ["UNBANDST"]=> string(1) "0" ["BANDST"]=> string(1) "0" ["VIEWBAN"]=> string(1) "0" ["UNRIPDST"]=> string(1) "0" ["RIPDST"]=> string(1) "0" ["VIEWRIP"]=> string(1) "0" ["VIEWXML"]=> string(2) "69" ["VIEWLOG"]=> string(1) "0" ["INVALID"]=> string(1) "3" } ["LISTENERS"]=> object(SimpleXMLElement)#5 (1) { ["LISTENER"]=> object(SimpleXMLElement)#7 (6) { ["HOSTNAME"]=> string(13) "78.129.222.56" ["USERAGENT"]=> string(11) "curl/7.29.0" ["UNDERRUNS"]=> string(1) "0" ["CONNECTTIME"]=> string(3) "216" ["POINTER"]=> string(1) "0" ["UID"]=> string(1) "2" } } ["SONGHISTORY"]=> object(SimpleXMLElement)#6 (1) { ["SONG"]=> object(SimpleXMLElement)#7 (2) { ["PLAYEDAT"]=> string(10) "1579791561" ["TITLE"]=> string(64) "BARRY WHITE - YOU'RE THE FIRST,THE LAST " } } }
NOTE: The problematic character is STILL THERE! \u0085 but properly encoded so I guess that is why it is not a problem any more...
I also tried the previous version of the code with the SimpleXMLElement constructor:
<?php
$output = file_get_contents('error.xml.bak');
$output = utf8_encode($output);
$xml = new SimpleXMLElement($output, LIBXML_NOERROR|LIBXML_ERR_NONE) or die('Something is wrong');
echo json_encode($xml);
?>
which also worked as expected:
{"CURRENTLISTENERS":"1","PEAKLISTENERS":"3","MAXLISTENERS":"5000","REPORTEDLISTENERS":"1","AVERAGETIME":"1","SERVERGENRE":"public","SERVERURL":"http:\/\/www.virtualdj.com\/","SERVERTITLE":"wolf","SONGTITLE":"BARRY WHITE - YOU'RE THE FIRST,THE LAST \u0085 ","SONGURL":{},"IRC":"wolf","ICQ":"wolf","AIM":"wolf","WEBHITS":"80","STREAMHITS":"6","STREAMSTATUS":"1","BITRATE":"96","CONTENT":"audio\/mpeg","VERSION":"1.9.8","WEBDATA":{"INDEX":"0","LISTEN":"0","PALM7":"6","LOGIN":"0","LOGINFAIL":"0","PLAYED":"0","COOKIE":"0","ADMIN":"1","UPDINFO":"1","KICKSRC":"0","KICKDST":"0","UNBANDST":"0","BANDST":"0","VIEWBAN":"0","UNRIPDST":"0","RIPDST":"0","VIEWRIP":"0","VIEWXML":"69","VIEWLOG":"0","INVALID":"3"},"LISTENERS":{"LISTENER":{"HOSTNAME":"78.129.222.56","USERAGENT":"curl\/7.29.0","UNDERRUNS":"0","CONNECTTIME":"216","POINTER":"0","UID":"2"}},"SONGHISTORY":{"SONG":{"PLAYEDAT":"1579791561","TITLE":"BARRY WHITE - YOU'RE THE FIRST,THE LAST \u0085 "}}}
NOTE the \u0085 towards the end...

Text file not written on the server

I try to write a file on my server, but I don't understand what's happening : it's always empty (blank page).
There is no error thrown.
When I insert inside the loop a var_dump($lines), I see the data but at the moment an error appear
With the code below
$minLat = -3.0000; //41.34343606848294;
$maxLat = 22.0000; //57.844750992891;
$minLng = -0.0300; //-16.040039062500004;
$maxLng = 90.4200; //29.311523437500004;
$step = 0.1;
$k = 1;
$estimator = new KNearestNeighbors(9);
$estimator->train($dataset->getSamples(), $dataset->getTargets());
$lines = [];
for($lat=$minLat; $lat<$maxLat; $lat+=$step) {
for($lng=$minLng; $lng<$maxLng; $lng+=$step) {
$lines[] = sprintf('%s;%s;%s', $lat, $lng, $estimator->predict([[$lat, $lng]])[0]);
}
}
var_dump($lines); ==> display info, but always empty
$filename = '/var/www/test/php-ml/result_map.csv';
//$content = implode(PHP_EOL, $lines);
$content = implode( "\n", $lines );
file_put_contents($filename, $content);
the result (example)
-------------
bool(true) ==> check if the file exist
array(1) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" }
array(2) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" }
array(3) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" [2]=> string(40) "-3;0.17;Apple 15 Inch MacBook Pro Laptop" }
array(4) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" [2]=> string(40) "-3;0.17;Apple 15 Inch MacBook Pro Laptop" [3]=> string(40) "-3;0.27;Apple 15 Inch MacBook Pro Laptop" }

Getting files from webpage and save with file_put_contents

I tried making a script that could save all files from a webpage with a pattern in name to a server.
<?php
$base_url = "http://www.someurl.com/";
$sufix = ".jpg";
$id_start= "Name Numberstart"; //Picture 1
$id_end = "Name Numberend"; // Picture 235
$path_to_save = "filer/";
foreach(range($id_start, $id_end) as $id) {
file_put_contents($path_to_save.$id.$sufix, file_get_contents($base_url.$id.$sufix));
}
?>
If I only use id with numbers etc. 1-20 or 50 - 60 and so on, the script works perfectly. But when I have a name in front and white space, it doesn't seem to work. It just saves 1 file and stops.
According to php.net's description of range:
Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.
I take that to mean that
range("Name Numberstart", "Name Numbersend") == array("N");
And if we run php interactively, this is confirmed:
php > $var = range("Name Numberstart", "Name Numbersend");
php > var_dump($var);
array(1) {
[0]=>
string(1) "N"
}
So basically, you're actually getting the range of the first character of the first word to the first character of the second word. If we change the second word to "Pretty", we get:
php > var_dump(range("Name Numberstart", "Pretty Numbersend"));
array(3) {
[0]=>
string(1) "N"
[1]=>
string(1) "O"
[2]=>
string(1) "P"
}

At least one output file must be specified

I am using this command in ffmpeg
ffmpeg -i test.flv -i /var/www/phpvibe/logo.gif -filter_complex "scale=1200:-1,overlay=0:0" -s 1280x720 -vcodec libx264 -s 640x360 -movflags test.mp4 2>&1
But i am getting this
string(42) "At least one output file must be specified"
updated:-
php code:-
$output ='{ffmpeg-cmd} -i {input} -i /var/www/phpvibe/logo.gif -filter_complex "scale=1200:-1,overlay=0:0" -s 1280x720 -vcodec libx264 -s {ffmpeg-vsize} -threads 4 -movflags {output}.mp4 2>&1';
Complete output:-
array(33) {
[0]=>
string(83) "ffmpeg version git-2015-05-11-94c20de Copyright (c) 2000-2015 the FFmpeg developers"
[1]=>
string(51) " built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)"
[2]=>
string(243) " configuration: --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3"
[3]=>
string(40) " libavutil 54. 23.101 / 54. 23.101"
[4]=>
string(40) " libavcodec 56. 38.100 / 56. 38.100"
[5]=>
string(40) " libavformat 56. 32.100 / 56. 32.100"
[6]=>
string(40) " libavdevice 56. 4.100 / 56. 4.100"
[7]=>
string(40) " libavfilter 5. 16.101 / 5. 16.101"
[8]=>
string(40) " libswscale 3. 1.101 / 3. 1.101"
[9]=>
string(40) " libswresample 1. 1.100 / 1. 1.100"
[10]=>
string(40) " libpostproc 53. 3.100 / 53. 3.100"
[11]=>
string(47) "Trailing options were found on the commandline."
[12]=>
string(31) "Input #0, flv, from 'test.flv':"
[13]=>
string(11) " Metadata:"
[14]=>
string(28) " audiosize : 150117"
[15]=>
string(26) " canSeekToEnd : true"
[16]=>
string(29) " datasize : 1015662"
[17]=>
string(26) " hasAudio : true"
[18]=>
string(27) " hasCuePoints : false"
[19]=>
string(26) " hasKeyframes : true"
[20]=>
string(26) " hasMetadata : true"
[21]=>
string(26) " hasVideo : true"
[22]=>
string(24) " lasttimestamp : 42"
[23]=>
string(72) " metadatacreator : flvtool++ (Facebook, Motion project, dweatherford)"
[24]=>
string(25) " totalframes : 229"
[25]=>
string(28) " videosize : 856032"
[26]=>
string(59) " Duration: 00:00:42.27, start: 0.066000, bitrate: 192 kb/s"
[27]=>
string(124) " Stream #0:0: Video: h264 (High), yuv420p, 176x144 [SAR 12:11 DAR 4:3], 165 kb/s, 30.30 fps, 29.97 tbr, 1k tbn, 59.94 tbc"
[28]=>
string(64) " Stream #0:1: Audio: aac (LC), 8000 Hz, stereo, fltp, 29 kb/s"
[29]=>
string(48) "Input #1, gif, from '/var/www/phpvibe/logo.gif':"
[30]=>
string(29) " Duration: N/A, bitrate: N/A"
[31]=>
string(67) " Stream #1:0: Video: gif, bgra, 90x55, 100 tbr, 100 tbn, 100 tbc"
[32]=>
string(42) "At least one output file must be specified"
}
int(1)
You put a -movflags without specifying a value before your output file. It thinks test.mp4 is the flag value hence your problem.

php getopt within bash script

I wrote a bash script to use a php script. The bash script creates an arguments list for the php script (as oppposed to plain passing the bash script's arguments to the php script.
My problem is that if I call the php script directly with the argument line generated by the bash script, (php) getopt works fine. Howhever, when the bash script attempts to execute the same line from within the script, (php) getopt fails!
here is the bash script:
#!/bin/sh
export PROFILE=$1
export TS_FILE=$2
if [ ! -z $PROFILE ] && [ -e $PROFILE ]; then
source $PROFILE
else
source default_profile
fi
if [ ! -z $TS_FILE ] && [ -e $TS_FILE ]; then
source $TS_FILE
else
source default_ts
fi
# create args for php script
ARGS_PHP="--agent \"$AGENT\" --sig \"$SIG_FILE\" --cie \"$CMPGY\" --super \"$SUPERVISOR\" --type \"$WRK_TYPE\" -d $DATES"
for nb_days in $(seq 0 $((DATES-1))); do
ARGS_PHP="$ARGS_PHP --date ${DATE_DATE[$nb_days]} --label \"${DATE_LABEL[$nb_days]}\" --as ${DATE_START[$i]} --pe ${DATE_END[$i]} --dt ${DATE_TOTAL[$i]}"
done
#echo ./script.php "$ARGS_PHP"
./script.php $ARGS_PHP
and here is the php script:
#!/usr/bin/php
<?php
$shortopts = "";
$shortopts .= "d:"; // Required value
$longopts = array(
"agent:", // Required value
"sig:", // Required value
"cie:", // Required value
"super:", // Required value
"type:", // Required value
"date:", // Required value
"label:", // Required value
"as:", // Required value
"pe:", // Required value
"dt:", // Required value
);
$options = getopt($shortopts, $longopts);
var_dump($options);
//var_dump($argv);
?>
If I execute the bash script as intended, I get:
array(1) {
["agent"]=>
string(5) ""Name"
}
If I uncomment the "echo" line and comment the php script call, I get:
./script.php --agent "Name Surname" --sig "../../path/sig.png" --cie "Cie Name" --super "Name Surname" --type "String" -d 5 --date 2014-03-31 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-01 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-02 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-03 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-04 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30
If I execute that line, I get:
array(11) {
["agent"]=>
string(12) "Name Surname"
["sig"]=>
string(18) "../../path/sig.png"
["cie"]=>
string(8) "Cie Name"
["super"]=>
string(12) "Name Surname"
["type"]=>
string(6) "String"
["d"]=>
string(1) "5"
["date"]=>
array(5) {
[0]=>
string(10) "2014-03-31"
[1]=>
string(10) "2014-04-01"
[2]=>
string(10) "2014-04-02"
[3]=>
string(10) "2014-04-03"
[4]=>
string(10) "2014-04-04"
}
["label"]=>
array(5) {
[0]=>
string(16) "Some label Value"
[1]=>
string(16) "Some label Value"
[2]=>
string(16) "Some label Value"
[3]=>
string(16) "Some label Value"
[4]=>
string(16) "Some label Value"
}
["as"]=>
array(5) {
[0]=>
string(4) "9:00"
[1]=>
string(4) "9:00"
[2]=>
string(4) "9:00"
[3]=>
string(4) "9:00"
[4]=>
string(4) "9:00"
}
["pe"]=>
array(5) {
[0]=>
string(5) "17:00"
[1]=>
string(5) "17:00"
[2]=>
string(5) "17:00"
[3]=>
string(5) "17:00"
[4]=>
string(5) "17:00"
}
["dt"]=>
array(5) {
[0]=>
string(4) "7:30"
[1]=>
string(4) "7:30"
[2]=>
string(4) "7:30"
[3]=>
string(4) "7:30"
[4]=>
string(4) "7:30"
}
}
If I comment the var_dump($options) and uncomment the var_dump($argv) I get the same behavior from the php script from the command line as from the bash script. Which is an array of all the (sub-)words from the $ARGS_PHP bash variable.
if I quote "$ARGS_PHP", I get:
array(0) {
}
with $options and
array(2) {
[0]=>
string(16) "./script.php"
[1]=>
string(490) "--agent "Name Surname" --sig "../../path/sig.png" --cie "Cie Name" --super "Name Surname" --type "String" -d 5 --date 2014-03-31 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-01 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-02 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-03 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30 --date 2014-04-04 --label "Some label Value" --as 9:00 --pe 17:00 --dt 7:30"
}
form $argv. How can I get the php script to use getopt properly when I launch from a bash script?!?!?!
PS:
default_profile:
export AGENT="Name Surname"
export SIG_FILE="../../path/sig.png"
default_ts:
#!/bin/sh
# default dummy values for TS (testing purposes)
# date solver. Default fills current week TS starting monday to friday
CURRENT=$(date +%u)
DOW=( "monday" "tuesday" "wednesday" "thursday" "friday")
function solve_date(){
THIS_DOW=$1
if [ $CURRENT -eq $THIS_DOW ];then
date +%F
elif [ $CURRENT -gt $1 ];then
date --date "last ${DOW[$((THIS_DOW-1))]}" +%F
else
date --date "next ${DOW[$((THIS_DOW-1))]}" +%F
fi
}
export DATES=$((5))
export CMPGY="Cie Name"
export SUPERVISOR="Name Surname"
export WRK_TYPE="String"
DATE_DATE[0]=$(solve_date 1)
DATE_LABEL[0]="Some label Value"
DATE_START[0]="9:00"
DATE_END[0]="17:00"
DATE_TOTAL[0]="7:30"
DATE_DATE[1]=$(solve_date 2)
DATE_LABEL[1]="Some label Value"
DATE_START[1]="9:00"
DATE_END[1]="17:00"
DATE_TOTAL[1]="7:30"
DATE_DATE[2]=$(solve_date 3)
DATE_LABEL[2]="Some label Value"
DATE_START[2]="9:00"
DATE_END[2]="17:00"
DATE_TOTAL[2]="7:30"
DATE_DATE[3]=$(solve_date 4)
DATE_LABEL[3]="Some label Value"
DATE_START[3]="9:00"
DATE_END[3]="17:00"
DATE_TOTAL[3]="7:30"
DATE_DATE[4]=$(solve_date 5)
DATE_LABEL[4]="Some label Value"
DATE_START[4]="9:00"
DATE_END[4]="17:00"
DATE_TOTAL[4]="7:30"
export DATE_DATE
export DATE_LABEL
export DATE_START
export DATE_END
export DATE_TOTAL
Because your list of arguments is all contained in a single string, bash is passing the whole string to php as a single argument, then PHP splits it on a space. The easiest way to fix is to call like this:
bash -c "php script.php $ARGS_PHP"
PHP can then evaluate the string of arguments as intended.

Categories