I have set the following on crontab
*/1 * * * * /opt/php-5.3/bin/php -f /var/httpd/appliance/appliance/docs/vip_dashboard_script.php >> /tmp/vip.log 2>&1
the contents of my script.
<?php
require (dirname(__FILE__).'/config.php');
use SMSPROXY\CurlTest;
$slug = 'vip/get-vip-performance';
$slug2 = 'vip/get-alert-recipients';
$slug3 = 'vip/update-alert-sent';
$curl = new CurlTest($slug);
$res = $curl->send();
for ($i = 0; $i < count($res); $i++) {
if($res[$i]->ALERT_SENT == 0){//send alert
$params = array(
"where"=>array(
"m.vip_msisdn" => $res[$i]->MSISDN,
"vp.CELL" => $res[$i]->CELL,
"vp.TIMESTAMP" => "to_date('".$res[$i]->TIMESTAMP."','dd-Mon-YY HH24:MI:SS')",
"vp.ERROR" => $res[$i]->ERROR,
)
);
$curl2 = new CurlTest($slug2,(ARRAY)$params);
$res2 = $curl2->send();
if(count($res2)>0){//if alerts exists
for ($j = 0; $j < count($res2); $j++) {
$id = str_replace (array('/',' ',':'),'',$res2[$j]->VIP_NAME) . $res2[$j]->VIP_MSISDN . str_replace (array('/',' ',':'),'',$res2[$j]->TIMESTAMP) . str_replace (array('/',' ',':'),'',$res2[$j]->ERROR) ;
$sender = $res2[$j]->R_MSISDN;
$response2 = $res2[$j]->VIP_NAME . " : " . $res2[$j]->VIP_MSISDN . " : " . $res2[$j]->TIMESTAMP . " : " . $res2[$j]->ERROR;
$response_counter=0;
while($response2 && $response_counter < 10) {
if(strlen($response2) < 160) {
$response3 = $response2;
$response2 = "";
$response_length= strlen($response3);
} else {
$response3 = preg_replace('/\s+?(\S+)?$/', '', substr($response2, 0, 160));
$response_length= strlen($response3);
$response3 = substr($response2,0,$response_length);
$response2 = substr($response2,$response_length);
}
if($response_length > 0) {
$cmd = "curl -sslv3 -k -d \"id=$id&source=4446&auth=4446&recipient=$sender&product=service-assurance&message=$response3&servicecode=SMS-Proxy\" -x proxy01.domain-is.de:8080 https://88.82.4.50/SMSAPI";
exec($cmd);
}
$response_counter++;
}
}
//update alert sent
$params = array(
"where"=>array(
"msisdn" => $res[$i]->MSISDN,
"cell" => $res[$i]->CELL,
"timestamp" => "to_date('".$res[$i]->TIMESTAMP."','dd-Mon-YY HH24:MI:SS')",
"error" => $res[$i]->ERROR,
)
);
$curl3 = new CurlTest($slug3,(ARRAY)$params);
$res3 = $curl3->send();
}// alert user end
}// end of alert check
}// end of for loop
and my class file
<?php
namespace SMSPROXY;
class CurlTest {
/**
*
* @constant useragent
*/
const USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
/**
*
* @constant base_url
*/
const BASE_URL = NULL;
/**
*
* @var curl instance
*/
protected $curl = NULL;
/**
*
* @var curl_debug
*/
protected $curl_debug = FALSE;
/**
*
* @var request method
*/
protected $url = NULL;
/**
* construct
*
* @return $void
*/
protected $params = array();
public function __construct($url='', $params = array(), $method='POST')
{
if (!extension_loaded('curl')) {
throw new \ErrorException('cURL library is not loaded');
}
$this->url = API_BASE_URL_TEST . API_SLUG_TEST . $url;
$this->params = $params;
$this->method = $method;
return $this;
}
/**
* make curl request
*
* @return $void
*/
public function send($url='', $params = array(), $method='')
{
if(!empty($url)) {
$this->url = API_BASE_URL_TEST . API_SLUG_TEST . $url;
}
if(!empty($params)) {
$this->params = $params;
}
if(!empty($method)) {
$this->method = $method;
}
return $this->init();
}
/**
* make curl request
*
* @return $void
*/
public function get($url='', $params = array())
{
if(!empty($url)) {
$this->url = $url;
} else {
die("URL REQUIRED " . __FILE__.__LINE__);
}
if(!empty($params)) {
$this->params = $params;
}
$this->method = 'GET';
return $this->init();
}
/**
* initialize curl connection
*
* @return $void
*/
public function init()
{
$this->curl = curl_init();
$this->setOption(CURLOPT_USERAGENT,self::USER_AGENT);
$this->setOption(CURLOPT_URL, $this->url);
$this->setOption(CURLINFO_HEADER_OUT, $this->curl_debug);
$this->setOption(CURLOPT_HEADER, $this->curl_debug);
$this->setOption(CURLOPT_RETURNTRANSFER, TRUE);
$this->setOption(CURLOPT_VERBOSE, $this->curl_debug);
$this->setOption(CURLOPT_COOKIESESSION, FALSE);
$this->setOption(CURLOPT_CUSTOMREQUEST, $this->method);
$this->setOption(CURLOPT_POSTFIELDS, http_build_query($this->params));
return $this->exec();
}
/**
* set curl options
*
* @return bool
*/
public function setOption($option, $value) {
return curl_setopt($this->curl, $option, $value);
}
/**
* exec
*
* @return reponse
*/
public function exec() {
print "\n\n<br>===============<br>\n\n";
print_r($this);
print "\n\n<br>===============<br>\n\n";
$response = curl_exec($this->curl);
if(!$this->curl->curlErrorCode ==0) {
print_r(curl_errno($this->curl));
print_r(curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
print_r(curl_getinfo($this->curl, CURLINFO_HEADER_OUT));
print_r($response);
die(__FILE__.__LINE__);
}
if(curl_error($this->curl))
{
print "\n\n<br>===============<br>\n\n";
print_r(curl_error($this->curl));
print "\n\n<br>===============<br>\n\n";
print_r(curl_errno($this->curl));
print "\n\n<br>===============<br>\n\n";
print_r(curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
print "\n\n<br>===============<br>\n\n";
print_r(curl_getinfo($this->curl, CURLINFO_HEADER_OUT));
print "\n\n<br>===============<br>\n\n";
print_r($response);
print "\n\n<br>===============<br>\n\n";
print_r($this->curl);
print "\n\n<br>===============<br>\n\n";
die(__FILE__.__LINE__);
}
//print_r($this->url);
//print_r($this->params);
//print_r($response);
//print "\n\n<br>===============<br>\n\n";
//die(__FILE__.__LINE__);
$fp = fopen(dirname(__FILE__).'/curltest.txt', 'a') or die("Cannot open file");
fwrite($fp, "===REQUEST===") or die("Cannot write to file");
fwrite($fp, print_r($this->params,1)) or die("Cannot write to file");
fwrite($fp, "\n") or die("Cannot write to file" . __FILE__.__LINE__);
fwrite($fp, "===RESPONSE===\n") or die("Cannot write to file");
//fwrite($fp, print_r(json_decode($response),true)) or die("Cannot write to file" . __FILE__.__LINE__);
//fwrite($fp, "\n") or die("Cannot write to file" . __file__.__line__);
fclose($fp);
//error_log(print_r(json_decode($response),true),3,dirname(__FILE__).'/curltest_before.txt');
//error_log(print_r($response,true),3,dirname(__FILE__).'/curltest_after.txt');
$response = json_decode($response);
if($response->error) {
die($response->message . __FILE__.__LINE__);
}
//print_r($response);
//die(__FILE__.__LINE__);
return $response->data;
}
}
?>
If I run this on CLI it works fine, however I cannot seem to figure out why it does not run on crontab.
Error I get in tmp/vip.log
is
<br>===============<br>
SMSPROXY\CurlTest Object
(
[curl:protected] => Resource id #9
[curl_debug:protected] => 1
[url:protected] => http://preprod.operationalintelligence.vf-uk.corp.domain.com/dev/uddins/anoc-api/vip/get-vip-performance
[params:protected] => Array
(
)
[method] => POST
)
<br>===============<br>
<br>===============<br>
couldn't connect to host
<br>===============<br>
7
<br>===============<br>
0
<br>===============<br>
<br>===============<br>
<br>===============<br>
Resource id #9
<br>===============<br>
After some further troubleshooting I have noticed the following problem
* About to connect() to preprod.operationalintelligence.vf-uk.corp.domain.com port 80
* Trying 10.97.18.39... * Connection timed out
* couldn't connect to host
* Closing connection #0
What don't understand is - I can do a wget
or a curl
from command line, however telnet from command line does not work on port 80.
http_proxy
orALL_PROXY
set when you work from CLI. If so, where are they set? In.bashrc
?.profile
? – RealSkeptic Mar 23 at 18:43.bashrc
/.profile
/etc. extensions, and ran the scripts? – Anthon Mar 26 at 5:31