2017-01-13 9 views
0

この問題をどのように解決するのか少し混乱します。 createSubaccount関数が失敗した場合、私たちはコード内でcreatesite関数を実行したくありません。私たちは、コードに関するフィードバック、コメント、ガイドを本当に感謝しています。関数b()が失敗したときに関数a()を終了します

<?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/>'); 
    } 
} 

?> 
+0

これは、[codereview.stackoverflow.com](http://codereview.stackoverflow.com)の方が良いかもしれません... –

答えて

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は、ブロック内の残りのコードが一度スローされるのを防ぎます。

関連する問題