是否可以在函数内调用函数?我在这里寻找解决方案。但老实说,我不知道如何在我的代码中实现它。
这是我第一次学习从头开始编写高级PHP代码,请帮我修改代码。
这是我的代码
<?php
function postdata($datatype){
if ($datatype == "processbalance"):
$site = "ADJUST BALANCE";
$signature = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$apikey);
$appSignature = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$signature.$appkey);
$params = array('transactId'=>$transactId,'cardNo'=>$cardNo,'amount'=>$amount,'transactionDate'=>$transactionDate,'partnerID'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
elseif ($datatype == "processreward"):
$site = "ADJUST POINT";
$signature = sha1($transactId.$cardNo.$reward.$transactionDate.$merchantID.$site.$apikey);
$appSignature = sha1($transactId.$cardNo.$reward.$transactionDate.$merchantID.$site.$signature.$appkey);
$params = array('transactId'=>$transactId,'cardNo'=>$cardNo,'reward'=>$reward,'items'=>$items,'transactionDate'=>$transactionDate,'merchantId'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
elseif ($datatype == 'processdeactivate'):
$site = "DEACTIVATE CARD";
$status = "False";
$signature = sha1($transactId.$oldnbc.$status.$status.$transactionDate.$merchantID.$site.$apikey);
$appSignature = sha1($transactId.$oldnbc.$status.$status.$transactionDate.$merchantID.$site.$signature.$appkey);
$params = array('transactId'=>$transactId,'cardNo'=>$oldnbc,'status'=>$status,'isGold'=>$status,'merchantDate'=>$transactionDate,'merchantId'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
endif;
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
$url = $url.implode('&', $encoded_params);
$arrContextOptions = array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false,),);
$url = file_get_contents($url, false, stream_context_create($arrContextOptions));
$getstatus = json_decode($url, true);
if ($getstatus['Status'] == 1):
echo $site." Succedd!";
else:
echo $site." Failed!";
endif;
}
function nbchd($type,$cardNo,$amount,$reward,$oldnbc){
if ($type == "migration" || $type == "adjustment"):
if ($amount != ""):
postdata("processbalance");
echo $result;
endif;
if ($reward != ""):
postdata("processreward");
echo $result;
endif;
elseif ($type == "replacement"):
if ($oldnbc != ''):
//post balance&reward to new nbchd
if ($amount != ""):
postdata("processbalance");
echo $result;
endif;
if ($reward != ""):
postdata("processreward");
echo $result;
endif;
//deactivating oldnbc
$cardNo = $oldnbc;
if ($amount != ""):
postdata("processbalance");
echo $result;
endif;
if ($reward != ""):
postdata("processreward");
echo $result;
endif;
postdata('processdeactivate');
echo $result;
else:
echo "Deactivating Error, Please fill OldNBC Number to continue";
endif;
endif;
}
date_default_timezone_set("Asia/Jakarta");
$apikey = "8ECDD8C5DF4647F0E6EB8CDEEC836E7DA8BACE5A3677424B";
$appkey = "27D5C651BBC94AE19BDF4F5E2B542667AE7C6EAE62D3BA34";
$transactId = "";
$url = "https://192.168.0.39:444/merchantwebapi/api/merchant?";
$submit = "Invoke";
$merchantDate = date("Y-m-d H:i:s", time());
$transactionDate= $merchantDate;
$merchantID = "BLITZ";
$merchantId = $merchantID;
$type = "adjustment";
$cardNo = "6048180000000017";
$amount = "10";
$reward = "1";
$oldnbc = "";
nbchd($type,$cardNo,$amount,$reward,$oldnbc);
?>
上面的代码在我的本地网络中运行以调用API函数,所以我很抱歉我不能将其作为工作代码发布。
运行代码后,我得到一些消息告诉我所有变量都是Undefined,例如:
注意:未定义的变量:cardNo in C:\ xampp \ htdocs \ nbchd \ function.php 在线 5
注意:未定义的变量:transactId in C:\ xampp \ htdocs \ nbchd \ function.php 在线 5
注意:未定义的变量:金额 C:\ xampp \ htdocs \ nbchd \ function.php 在线 5
注意:未定义的变量:transactionDate in C:\ xampp \ htdocs \ nbchd \ function.php 在线 5
注意:未定义的变量:商家ID in C:\ xampp \ htdocs \ nbchd \ function.php 在线 5
@ john-conde我认为这个问题与PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"不一样。调用函数时,变量丢失了。这段代码在直接写入时起作用,而不是从函数中写入。
这是我的简单代码
<?php
date_default_timezone_set("Asia/Jakarta");
$apikey = "8ECDD8C5DF4647F0E6EB8CDEEC836E7DA8BACE5A3677424B";
$appkey = "27D5C651BBC94AE19BDF4F5E2B542667AE7C6EAE62D3BA34";
$transactId = "";
$url = "https://192.168.0.39:444/merchantwebapi/api/merchant?";
$submit = "Invoke";
$merchantDate = date("Y-m-d H:i:s", time());
$transactionDate= $merchantDate;
$merchantID = "BLITZ";
$merchantId = $merchantID;
$type = "adjustment";
$cardNo = "6048180000000017";
$amount = "10";
$reward = "1";
$oldnbc = "";
if ($type == "adjustment"):
if ($amount != ""):
$site = "ADJUST BALANCE";
$signature = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$apikey);
$appSignature = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$signature.$appkey);
$params = array('transactId'=>$transactId,'cardNo'=>$cardNo,'amount'=>$amount,'transactionDate'=>$transactionDate,'partnerID'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
$url = $url.implode('&', $encoded_params);
$arrContextOptions = array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false,),);
$url = file_get_contents($url, false, stream_context_create($arrContextOptions));
$getstatus = json_decode($url, true);
if ($getstatus['Status'] == 1):
echo $site." Succedd!";
else:
echo $site." Failed!";
endif;
endif;
endif;
?>