PHP:首先调用POST时,行运行两次

时间:2016-02-07 02:30:05

标签: php cron

我在脚本中有几行代码,如果A部分执行,则B部分执行TWICE(即:cron.php文件被添加到crontab TWICE)。这很奇怪,因为如果我注释掉A部分,B部分只执行ONCE。

为什么会发生这种情况?我错过了什么?

// Part A
$url = "https://api.sendgrid.com/apiv2/customer.add.json";
$input = "api_user=$sendgrid_user_account&api_key=$sendgrid_master_password&username=$custname.domain.com&website=$sendgridsubuserdomain&password=$sendgridsubusersmtppass&con    firm_password=$sendgridsubusersmtppass&first_name=$first&last_name=$last&address=$sendgridsubuseraddress&city=$sendgridsubusercity&state=$sendgridsubuserstate&zip=$    sendgridsubuserzip&email=$email&country=$sendgridsubusercountry&company=$custname&phone=$sendgridsubuserphone";
$sendgrid_output = postCurl($url, $input, false, false);

// Part B
chdir("/etc/nginx/");
exec("2>&1 ./addcron.sh $custname");

支持功能:

function postUrl($url, $data, $headers = null, $json_format = true) { // json format is only used if $data is also formatted as a string
    $curl_result = postCurl($url, $data);
    if($curl_result != false) return $curl_result;

    $data_query = http_build_query($data);
    $opts = array('http' => array('method' => 'POST', 'content' => $data_query));

    if($headers) {
        $opts['http']['header'] = $headers;
    }
    else {
        $opts['http']['header'] = "Content-type: application/x-www-form-urlencoded";
}
    $st = stream_context_create($opts);
    $fp = fopen($url, 'rb', false, $st);

    if(!$fp) {
        //$result = http_post_fields($url, $data); TODO: add back in once AWS's PHP updated to include http_post_fields function
         //if(!empty($result)) return $result;
    }
    $result = stream_get_contents($fp);
    if(empty($result)) {
        //$result = http_post_fields($url, $data);
        //if(!empty($result)) return $result;
    }

    return false; // if all else fails, false
}

function postCurl($url, $values, $boolean_return = false, $json_format = true) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    if(is_array($values)) {
        $values_string = "";
        foreach($values as $key => $value) $values_string .= "$key=$value&";
        $values_string = substr($values_string, 0, -1); // remove last "&"
    }
    else { // if it's not an array, assume JSON
        $values_string = $values;
        if($json_format == true) {
                $input_array = array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($values_string));
        }
        else {
            $input_array = array('Content-Length: ' . strlen($values_string));
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $input_array);
    }

    curl_setopt($ch, CURLOPT_POSTFIELDS, $values_string);

    // in real life you should use something like:
    // curl_setopt($ch, CURLOPT_POSTFIELDS,
    //          http_build_query(array('postvar1' => 'value1')));

    // receive server response ...
    if($boolean_return == false) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);
    curl_close ($ch);

    return $server_output;
}

addcron.sh脚本:

#!/bin/bash
custname="$1"
(crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" ) | crontab -

0 个答案:

没有答案