ข้อมูลทดสอบ / ตัวอย่าง Code PHP แบบง่าย


<?php

  
/* ##### ประกาศ Function postJSON ##### */
  
function postJSON($postData){
    
$url "https://api.josoco.com/post";
    
$ch curl_init($url);
    
$data_string json_encode($postData);
    
curl_setopt($chCURLOPT_POSTtrue);
    
curl_setopt($chCURLOPT_POSTFIELDS, array("shipping"=>$data_string));
    
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    
$output curl_exec ($ch); // execute
    
if($output === false)
    {
        echo 
"Error Number:".curl_errno($ch)."<br>";
        echo 
"Error String:".curl_error($ch);
    }else{
    
curl_close($ch);
    return 
$output;
    }
  } 
/* ##### สิ้นสุด Function ##### */

  /* การใช้งาน สร้างรายการสิ้นค้าทั้งหมดของ Order นั้น ๆ จัดเรียงเป็น Array*/
  
$data = array(
      
"key" => "4c3373935ebb31bd73ad8779daff5d2e",
      
"provider" => "4",
      
"zipcode"=>"23170",
      
"items" => array(
                  array(
"sku"=>"sku001","st"=>"1","w"=>"30","l"=>"30","h"=>"30","weight"=>"10","box"=>"1"),
                  array(
"sku"=>"sku002","w"=>"30","l"=>"30","h"=>"30","weight"=>"10","box"=>"1"),
                  array(
"sku"=>"sku003","w"=>"30","l"=>"30","h"=>"30","weight"=>"10","box"=>"1")
                      )
      );
    
$result json_decode(postJSON($data), TRUE);
    echo 
"<p>1. คืนค่าแบบ JSON</p>";
    echo 
"<pre>";
    
print_rpostJSON($data) );
    echo 
"</pre>";

    echo 
"<br><p>2. คืนค่าแบบ Array</p>";
    echo 
"<pre>";
    
print_r$result );
    echo 
"</pre>";
    echo 
"<br><font color=\"green\">คืนผลลัพธ์ : ".$result["shipping"]["total"]."</font>";
    
/* สิ้นสุดการใช้งาน */
?>

ผลลัพธ์หลังเรียก Function

1. คืนค่าแบบ JSON

{"shipping":{"code":"200","msg":"ok","items":{"sku001":{"price":480,"zone":0},"sku002":{"price":480,"zone":0},"sku003":{"price":480,"zone":0}},"charge":0,"subtotal":1440,"discount":1040,"total":400}}

2. คืนค่าแบบ Array

Array
(
    [shipping] => Array
        (
            [code] => 200
            [msg] => ok
            [items] => Array
                (
                    [sku001] => Array
                        (
                            [price] => 480
                            [zone] => 0
                        )

                    [sku002] => Array
                        (
                            [price] => 480
                            [zone] => 0
                        )

                    [sku003] => Array
                        (
                            [price] => 480
                            [zone] => 0
                        )

                )

            [charge] => 0
            [subtotal] => 1440
            [discount] => 1040
            [total] => 400
        )

)

หมายเหตุ : หากคืนค่า code:200 คือการดึงค่าสำเร็จแล้ว

คืนผลลัพธ์ : 400