【问题标题】:Convert PHP http post request to Livecode将 PHP http post 请求转换为 Livecode
【发布时间】:2018-04-01 08:56:59
【问题描述】:

我是 Livecode 的新手,我尝试了一些方法来将此 php http 发布请求代码转换为 Livecode,但无法正常工作。使用 cURL 或不使用 cURL 都需要它。

    $receive_momo_request = array(
         'CustomerName' => 'Customer Name',
      'CustomerMsisdn'=> '054XXXX',
      'CustomerEmail'=> 'customer@gmail.com',
      'Channel'=> 'mtn-gh',
      'Amount'=> 0.8,
      'PrimaryCallbackUrl'=> 'http://requestb.in/1minotz1',
      'Description'=> 'T Shirt',

);

//API Keys

$clientId = 'xxxxxxx';
$clientSecret = 'xxxxxxx';
$basic_auth_key =  'Basic ' . base64_encode($clientId . ':' . $clientSecret);
$request_url = 'https://api.hubtel.com/v1/merchantaccount/merchants/HMXXXXXXX/receive/mobilemoney';
$receive_momo_request = json_encode($receive_momo_request);

$ch =  curl_init($request_url);  
        curl_setopt( $ch, CURLOPT_POST, true );  
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $receive_momo_request);  
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Authorization: '.$basic_auth_key,
            'Cache-Control: no-cache',
            'Content-Type: application/json',
          ));

$result = curl_exec($ch); 
$err = curl_error($ch);
curl_close($ch);

if($err){
    echo $err;
}else{
    echo $result;
}

这是我到目前为止所做的,可能缺少一些东西。

on mouseUp
   global gFirstName, gLastName
   put gFirstName & " " & gLastName into lFullName
   put lFullName into tArray[ "CustomerName"]
   put "gPhoneNumber" into tArray["CustomerMsisdn"]
   put "gEmail"  into tArray["CustomerEmail"]
   put "airtel-gh" into tArray["Channel"]
   put "0.01" into tArray["Amount"]
   put "http://requestb.in/1minotz1" into tArray["PrimaryCallbackUrl"]
   put "FBMC Mobile" into tArray["Description"]
   put true into tArray ["FeesOnCustomer"]

   put ArrayToJSON(tArray) into receive_momo_request

   put "ABCD" into clientId
   put "1234" into clientSecret
   set the httpHeaders to "Content-type: application/json" && "Authorization: Basic " && base64Encode("clientId:clientSecret") && "Cache-Control: no-cache"
   post receive_momo_request to url "https://api.hubtel.com/v1/merchantaccount/merchants/HMXXXXXXX/receive/mobilemoney"

end mouseUp

【问题讨论】:

  • 你能贴出你尝试过的代码吗?
  • 我已经发布了到目前为止我尝试过的内容
  • 发布任何错误或 - 您观察到它不起作用的情况会有所帮助。有错误吗?你怎么知道它不起作用?

标签: php http http-post livecode


【解决方案1】:

您的 LiveCode 代码乍一看还不错。我会尝试两件事:

首先,在发布之前对您发布的数据进行 URLencode。

put ArrayToJSON(tArray) into receive_momo_request
put urlEncode(receive_momo_request) into receive_momo_request

其次,在post 命令之后,检查it 变量以查看Web 服务器返回了哪些数据。您也可以查看the result,看看是否发生了错误。

post receive_momo_request to url "https://api.hubtel.com/v1/merchantaccount/merchants/HMXXXXXXX/receive/mobilemoney"
put it into tServerFeedback
answer the result

这至少应该告诉你发出post 命令后发生了什么。

【讨论】:

  • 来自服务器的响应:{"ResponseCode":"4101","Message":"Permission denied"}put "Accept: application/json" &&"Authorization: Basic" && base64Encode("clientId:clientSecret") && "Cache-Control: no-cache" &&"Content-type: application/json" into tHeader set the httpHeaders to tHeader 我相信问题可能出在标题上,但不确定是什么问题,但身份验证密钥都是正确的,不知道为什么服务器拒绝访问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多