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

PHP集成PayMaya时用Curl创建客户无响应问题求助

Troubleshooting PayMaya Customer Creation via PHP cURL + SDK Namespace Issue

Hey there! I’ve worked on PayMaya integrations before and ran into similar hurdles, so let’s break down your problems step by step.

First: Fixing the Empty cURL Response

Your main issue right now is the malformed request body and a cURL setting that’s hiding error details. Here’s what’s wrong and how to fix it:

1. Wrong Request Body Format

You’re using http_build_query($body) to generate the POST data, but PayMaya’s API expects JSON-formatted payloads (matching your Content-Type: application/json header). http_build_query creates application/x-www-form-urlencoded data, which the API can’t parse—this is why you’re getting an empty response. Replace it with json_encode($body).

2. CURLOPT_FAILONERROR Hides Errors

When this option is enabled, curl_exec() returns FALSE immediately if the API returns a 4xx/5xx status code. You lose the chance to see the actual error message from PayMaya (like invalid auth, missing fields, etc.). Disable this setting to capture full error responses.

3. Add Debugging to Diagnose Issues

Add checks for cURL errors and request info to pinpoint exactly what’s going wrong.

Here’s your revised createCustomer() method:

public function createCustomer(){
    $this->paymayaInit();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json", 
        "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="
    ));
    // Disable FAILONERROR to see API error responses
    // curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    
    $body = array(
        "firstName" => "Ysa",
        "middleName" => "Cruz",
        "lastName" => "Santos",
        "birthday" => "1987-10-10",
        "sex" => "F",
        "contact" => array(
            "phone" => "+63(2)1234567890",
            "email" => "ysadcsantos@gmail.com"
        ),
        "billingAddress" => array(
            "line1" => "9F Robinsons Cybergate 3",
            "line2" => "Pioneer Street",
            "city" => "Mandaluyong City",
            "state" => "Metro Manila",
            "zipCode" => "12345",
            "countryCode" => "PH"
        ),
        "metadata" => array()
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    // Use json_encode for correct JSON payload
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    
    $response = curl_exec($ch);
    
    // Add debugging logs
    if($response === FALSE){
        echo "cURL Error: " . curl_error($ch);
        echo "\nRequest Details: " . print_r(curl_getinfo($ch), TRUE);
    } else {
        echo "API Response: " . $response;
    }
    
    curl_close($ch);
    return json_decode($response);
}

Second: Fixing the SDK Namespace Issue

You don’t need to comment out the SDK’s namespace— that’s a hack and will cause issues later. Instead, use PHP’s use statement to properly import the SDK classes:

  1. Make sure your autoloader path is correct (Composer’s autoloader should handle namespace resolution if you set it up right).
  2. Add the use statement at the top of your file to import the SDK class:
require_once(DIR_VENDOR . 'PayMaya-PHP-SDK-master/sample/autoload.php');
// Import the SDK namespace (adjust if your SDK uses a different namespace)
use PayMaya\PayMayaSDK;

Class Paymaya {
    public function paymayaInit(){
        PayMayaSDK::getInstance()->initCheckout(
            "pk-nRO7clSfJrojuRmShqRbihKPLdGeCnb9wiIWF8meJE9", 
            "sk-jZK0i8yZ30ph8xQSWlNsF9AMWfGOd3BaxJjQ2CDCCZb", 
            "SANDBOX"
        );
    }
    // ... rest of your methods
}

Pro Tip: Use the SDK’s Built-in Methods

Since you’re already including the SDK, skip writing raw cURL and use the SDK’s customer creation methods instead—it’s less error-prone:

public function createCustomerViaSDK(){
    $this->paymayaInit();
    
    // Initialize customer model (adjust namespace if needed)
    $customer = new \PayMaya\Model\Customer();
    $customer->firstName = "Ysa";
    $customer->middleName = "Cruz";
    $customer->lastName = "Santos";
    $customer->birthday = "1987-10-10";
    $customer->sex = "F";
    
    // Add contact details
    $contact = new \PayMaya\Model\Contact();
    $contact->phone = "+63(2)1234567890";
    $contact->email = "ysadcsantos@gmail.com";
    $customer->contact = $contact;
    
    // Add billing address
    $billingAddress = new \PayMaya\Model\BillingAddress();
    $billingAddress->line1 = "9F Robinsons Cybergate 3";
    $billingAddress->line2 = "Pioneer Street";
    $billingAddress->city = "Mandaluyong City";
    $billingAddress->state = "Metro Manila";
    $billingAddress->zipCode = "12345";
    $billingAddress->countryCode = "PH";
    $customer->billingAddress = $billingAddress;
    
    $customer->metadata = [];
    
    try {
        $response = $customer->create();
        print_r($response);
        return $response;
    } catch (\Exception $e) {
        echo "SDK Error: " . $e->getMessage();
    }
}

Final Notes

  • Start with fixing the cURL payload and debugging to get clear error messages from PayMaya.
  • Stick to the SDK’s namespace system instead of modifying the SDK code—it’ll make future updates easier.
  • Always test in the sandbox environment first, and double-check your API keys are correct.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:58:14