PHP Curl ไม่ยอมรับตัวแปร

0

คำถาม

ฉันพยายามบางอาร์กิวเมนต์ใน Curl แต่ฉันรู้ตอนที่ผมใช้ทดสอบข้อมูล(เรียบง่ายใช้เส้นสาย)ฉันไปเอาผล(payId และ payUrl)แต่ตอนที่ฉันใช้ตัวแปรแทนที่จะเป็นบททดสอบข้อมูลฉันจะไม่กลับมา

อะไรคือหนทางที่ดีที่สุดที่จะ troubleshoot นี้ประเภทของกษาที่สถาบันเหมือนกันนะ มันเป็นครั้งแรกของฉันโดยใช้ Curl ดังนั้นฉันกำลังสร้างฟังก์ชันนี้จากข่าวที่ฉันเจออยู่ที่นี่

อย่างเช่นถ้าฉันแทน"ชื่อ"กับ$clientName ฉันไม่ได้กลับมาจากรูปแบบ api.

function generatePayment($sendId, $clientCPF, $clientName, $clientEmail, $clientCep, $clientPhone, $amount){
    
    
    // CONFIG
    $urlCallBack = "http://192.168.0.79/sistema/admin/shipList.php?transactionStatus=success";
    $urlError = "http://192.168.0.79/sistema/admin/shipList.php?transactionStatus=failed";
    $debug = true;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'urlhere');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\t\"reference\": \"my-order-ref-0001\",\n\t\"client\": {\n    \t\"cpf\": \"43164853858\",\n    \t\"name\": \"John Doe\",\n    \t\"email\": \"[email protected]\",\n    \t\"birthdate\": \"1982-01-14\",\n    \t\"cep\": \"18053190\",\n    \t\"phone\": \"15987452584\"\n\t},\n\t\"items\": [\n    \t{\n        \t\"reference\": \"my-item-ref-0001\",\n        \t\"description\": \"Apple iMac 21.5-inch\",\n        \t\"quantity\": \"1\",\n        \t\"amount\": 149900\n    \t},\n    \t{\n        \t\"reference\": \"my-item-ref-0002\",\n        \t\"description\": \"Apple iPhone 11 Pro\",\n        \t\"quantity\": 1,\n        \t\"amount\": 99900\n    \t}\n\t],\n     \"coupon\": {\n         \"code\": \"10DESCONTO\",\n         \"value\": 1000,\n         \"issuer\": \"merchant_api\"\n     },\n\t\"shipping\": {\n           \"amount\": 1000\n\t},\n\t\"redirect\": {\n    \t      \"success\": \"https://example.com/sp_callback?success\",\n    \t      \"failed\": \"https://example.com/sp_callback?failed\"\n\t}\n}");
    
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: Bearer '.$_POST['token'];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        if ($debug){
            echo "<BR><BR><BR> DATA PASSED TO FUNCTION <BR>";
            echo "<br>sendId ===> ".$sendId;
            echo "<br>clientCPF ===> ".$clientCPF;
            echo "<br>clientName ===> ".$clientName;
            echo "<br>clientEmail ===> ".$clientEmail;
            echo "<br>clientCep ===> ".$clientCep;
            echo "<br>clientPhone ===> ".$clientPhone;
            echo "<br>amount ===> ".$amount;
            
            $decode = json_decode($result,true);
            
            echo "<BR><BR> DATA RECEIVED FROM API <BR>";
            echo '<br> payId generated by API ==> '.$payId = $decode['data']['order_id'];
            echo '<br> PayURL generated by API ==>'.$payUrl = $decode['data']['url_checkout'];

        } else {
            $decode = json_decode($result,true);
            $transactionId = $decode['data']['order_id'];
            $payUrl = $decode['data']['url_checkout'];
            $_SESSION['transactionUrl'] = $decode['data']['url_checkout'];
            $_SESSION['transactionId'] = $transactionId;
            $_SESSION['sendId'] = $sendId;
        }
    }

    curl_close($ch);

    // END GENERATE PAYMENT
}

ช่วยเป็นเกียรติอย่างยิ่งอย่างต้อง troubleshoot.


กำลังปรับปรุงรหัสโดยใช้ LANGUAGE


//INITIALIZE
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://sandbox.splipay.com/api/orders');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    // POST
 
    $post = array(
            "items" => array (
                "description" => "Test",
                "quantity" => 1,
                "amount" => 10
            ),
            "redirect" => array (
                "success" => "www.success",
                "failed" => "www.success"
            )
        );
    
    $post = json_encode($post);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    
    // HEADERS
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: Bearer '.$_POST['token'];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    //EXECUTE
    $result = curl_exec($ch);
    
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        $arr = json_decode($result);
        echo "<br><br>Return from VAR_DUMP";
        var_dump ($arr);
        echo "<BR><BR>INFO RETURN<BR>";
        $info = curl_getinfo($ch);
        var_dump ($info);
        
        // $_POST['token'] = $arr['access_token'];
        
    }
    curl_close($ch);

เป็นส่วนหนึ่งเอกสารพวกนี้เป็นเพียงค่าของฉันต้องการที่จะส่งไปให้รูปแบบ api และฉันอยากจะติดตามการตอบสนอ(ตัวอย่าง):

{ "ความสำเร็จ":เรื่องจริง "ข้อมูล":{ "order_id":57, "url_checkout":"https://sandbox.splipay.com/payment/4openRe7Az/kzPdyP7bQr?expires=1583164527&signature=e1caae419137903f930f5a5b3d4c72608a61c0fdd36c70803d4a92c15556a4c5" } }

api curl php
2021-11-23 22:15:38
1

คำตอบที่ดีที่สุด

0

หลังจากการย้ายเพื่อน LANGUAGE รูปแบบและรับการตรวจสอบการที่รูปแบบ api ความต้องการฉันสามารถแก้ไขเรื่องนี้ ครั้งหนึ่งใน LANGUAGE มันง่ายที่จะระบุว่ามีปัญหากับหลายไปสู่มิติอาเรย์. ฉัน rewrote รหัสและมันเป็นงานแล้ว ขอบคุณทุกคน

LANGUAGE

$post = array(
            "reference" => "my-order-ref-0001",
            "client" => array (
                "cpf" => "43164853858",
                "name" => "John Doe",
                "email" => "[email protected]",
                "birthdate" => "1982-01-14",
                "cep" => "18053190",
                "phone" => "15987452584"
            ),
            "items" => array (
                array (
                    "reference" => "my-item-ref-0001",
                    "description" => "Test",
                    "quantity" => 1,
                    "amount" => 10
                ),
            ),
            "coupon" => array(
                "code" => "10DESCONTO",
                "value" => 1000,
                "issuer" => "merchant_api"
            ),
            "shipping" => array (
                "amount"=> 1000
            ),
            "redirect" => array (
                "success" => "www.success",
                "failed" => "www.success"
            )
        );
2021-11-24 14:13:48

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่