You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP HTTP POST请求转Livecode实现求助(支持cURL/非cURL)

Hey there! I see you're new to LiveCode and struggling to convert that PHP POST request over—let's fix that. I've got two reliable solutions for you: one that uses LiveCode's built-in HTTP tools (no cURL required) and another that leverages cURL if you prefer that approach.

First, let's recap what your PHP code is doing: it's constructing a data payload, then (I assume) sending it as a JSON POST request to an API with client ID/secret authentication. Let's replicate that in LiveCode.

Option 1: No cURL Needed (LiveCode Built-in HTTP)

LiveCode has native HTTP functions that work great for most cases. We'll use revHTTPPost and handle JSON formatting manually (since LiveCode doesn't have a built-in array-to-JSON converter, but it's easy to build the string):

on sendMomoRequest
    -- 1. Define your request data
    put "Customer Name" into tCustomerName
    put "054XXXX" into tCustomerMsisdn
    put "customer@gmail.com" into tCustomerEmail
    put "mtn-gh" into tChannel
    put 0.8 into tAmount
    put "http://requestb.in/1minotz1" into tCallbackUrl
    put "T Shirt" into tDescription
    
    -- 2. Build the JSON request body
    put "{" & quote & "CustomerName" & quote & ":" & quote & tCustomerName & quote & "," & \
        quote & "CustomerMsisdn" & quote & ":" & quote & tCustomerMsisdn & quote & "," & \
        quote & "CustomerEmail" & quote & ":" & quote & tCustomerEmail & quote & "," & \
        quote & "Channel" & quote & ":" & quote & tChannel & quote & "," & \
        quote & "Amount" & quote & ":" & tAmount & "," & \
        quote & "PrimaryCallbackUrl" & quote & ":" & quote & tCallbackUrl & quote & "," & \
        quote & "Description" & quote & ":" & quote & tDescription & quote & "}" into tRequestBody
    
    -- 3. Set up API authentication (assuming Basic Auth; adjust if your API uses Bearer tokens)
    put "xxxxxxx" into tClientId
    put "your_client_secret_here" into tClientSecret
    put base64Encode(tClientId & ":" & tClientSecret) into tAuthToken
    put "Authorization: Basic " & tAuthToken & cr & "Content-Type: application/json" into tHeaders
    
    -- 4. Replace with your actual API endpoint URL
    put "https://your-api-endpoint.com" into tApiUrl
    
    -- 5. Send the POST request
    revHTTPPost tApiUrl, tRequestBody, tHeaders
    
    -- 6. Handle the response
    put the result into tError
    if tError is empty then
        put it into tResponseData
        answer "Request succeeded!" & cr & cr & "Response:" & cr & tResponseData
    else
        answer "Request failed:" & cr & cr & tError
    end if
end sendMomoRequest

Notes for Option 1:

  • If your API expects form data instead of JSON, change the Content-Type header to application/x-www-form-urlencoded and format the body like CustomerName=Customer+Name&CustomerMsisdn=054XXXX instead of JSON.
  • Adjust the authentication header if your API uses a different method (e.g., Authorization: Bearer YOUR_TOKEN for OAuth).
  • You can set a timeout with libUrlSetOption "timeout", 30 (sets 30-second timeout) before calling revHTTPPost.

Option 2: Using cURL via Shell Command

If you need to use cURL (for example, if you rely on specific cURL flags), you can call the system's cURL executable directly from LiveCode:

on sendMomoRequestWithCurl
    -- 1. Define your request data
    put "Customer Name" into tCustomerName
    put "054XXXX" into tCustomerMsisdn
    put "customer@gmail.com" into tCustomerEmail
    put "mtn-gh" into tChannel
    put 0.8 into tAmount
    put "http://requestb.in/1minotz1" into tCallbackUrl
    put "T Shirt" into tDescription
    
    -- 2. Build the JSON request body
    put "{" & quote & "CustomerName" & quote & ":" & quote & tCustomerName & quote & "," & \
        quote & "CustomerMsisdn" & quote & ":" & quote & tCustomerMsisdn & quote & "," & \
        quote & "CustomerEmail" & quote & ":" & quote & tCustomerEmail & quote & "," & \
        quote & "Channel" & quote & ":" & quote & tChannel & quote & "," & \
        quote & "Amount" & quote & ":" & tAmount & "," & \
        quote & "PrimaryCallbackUrl" & quote & ":" & quote & tCallbackUrl & quote & "," & \
        quote & "Description" & quote & ":" & quote & tDescription & quote & "}" into tRequestBody
    
    -- 3. Set up API authentication
    put "xxxxxxx" into tClientId
    put "your_client_secret_here" into tClientSecret
    put base64Encode(tClientId & ":" & tClientSecret) into tAuthToken
    
    -- 4. Build the cURL command (adjust path to curl.exe if on Windows)
    put "curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Basic " & tAuthToken & "' -d '" & tRequestBody & "' 'https://your-api-endpoint.com'" into tCurlCmd
    
    -- 5. Execute the command and capture the response
    do shell tCurlCmd
    put it into tResponse
    
    -- 6. Show the result
    answer "cURL Response:" & cr & cr & tResponse
end sendMomoRequestWithCurl

Notes for Option 2:

  • Make sure cURL is installed on the system running LiveCode (most macOS/Linux systems have it pre-installed; Windows may require adding it to your PATH or using the full path like C:\Windows\System32\curl.exe).
  • If your JSON has special characters, you might need to escape them properly for the shell.

内容的提问来源于stack exchange,提问作者Ebenezer Eshun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:01:31