当函数b()失败时终止函数a()

时间:2017-01-13 01:46:00

标签: php curl

我们对如何解决这个问题感到有点困惑。如果createSubaccount函数失败,我们不想在代码中运行createsite函数。我们非常感谢您对我们代码的任何反馈,评论和指南。

<?php
//Set API user and password
define("API_USER","user");
define("API_PASS","pw");

    $createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
    //echo 'Site Created: ' . $createdSite . '<br/>';
    $accountCreated = createSubAccount($_REQUEST['email']);//client email
    //echo 'Account created: ' . $accountCreated . '<br/>';


    $first_name = $_REQUEST['first_name'];//First Name
    $last_name = $_REQUEST['last_name'];//Last Name

    $retArr = ["sso"=>$sso_link,"ru"=>$resetURL,"ac"=>$accountCreated,"fn"=>$first_name,"ln"=>$last_name];//assoc array

    print json_encode ($retArr);//json string


function createSite($template_id,$original_url) {
    //create array with data
    if($original_url) {
        $data = array("template_id"=>$_REQUEST['template_id'],"url"=>$original_url);    
    } else {
        $data = array("template_id"=>$_REQUEST['template_id']);
    }
    //turn data into json to pass via cURL
    $data = json_encode($data);
    //Set cURL parameters
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call and get template data
    $output = curl_exec($ch);
    //check for errors in cURL
    if(curl_errno($ch)) {
        die('Curl error: ' . curl_error($ch));
    }
    $output = json_decode($output);
    return $output->site_name;//Output /Return :  {"site_name":"28e1182c"}
}

function createSubAccount($emailToCreate) {
    $first_name = $_REQUEST['first_name'];//First Name
    $last_name = $_REQUEST['last_name'];//Last Name
    $data = '{"account_name":"'.$emailToCreate.'", "first_name":"'.$first_name.'", "last_name":"'.$last_name.'"}';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call and get template data
    $output = curl_exec($ch);
    if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
        curl_close($ch);
        return $emailToCreate;//Expected return HTTP Code: 204 No Content
    } else {
        curl_close($ch);
        $output = 'failed';
        return $output;
        die('Account creation failed, error: '. $output . '<br/>');
    }
}

?>

1 个答案:

答案 0 :(得分:0)

这是Exception派上用场的地方。

function createSite() {
     throw new \Exception('Failed');
}

try {
    $createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
    //echo 'Site Created: ' . $createdSite . '<br/>';
    $accountCreated = createSubAccount($_REQUEST['email']);//client email
} catch(\Exception $err) {
     echo $err->getMessage();
}

Exception阻止块中的其余代码在抛出后执行。