使用cURL请求Api呼叫

时间:2017-02-22 14:57:25

标签: php json api class curl

有人可以帮助我吗?我试图通过下面的代码提出请求,但任何事情都会发生,任何消息都会出现。我相信我的代码是正确的:

public function subscribe(){   
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';  

$json_string  = json_encode(array(     

"MerchantOrderId"=>"2014113245231706",
 "Customer" => array(
    "Name" => "Comprador rec programada"
 ),

 "Payment" => array(
    "Type" => "CreditCard",
    "Amount" => 1500,
    "Installments" => 1,
     "SoftDescriptor" => "Assinatura Fraldas"  
 )    


 ));


$ch = curl_init($json_url);

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);

 curl_setopt_array( $ch, $options );
 $result = curl_exec($ch); // Getting jSON result string

 print_r($result);                

}

查找网站说明的链接:

2 个答案:

答案 0 :(得分:1)

你会想到这个:

   [
  {
    "Code": 114,
    "Message": "The provided MerchantId is not in correct format"
  }
]

使用此代码:

function subscribe(){   
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';  
$json_string  = json_encode(
    array(     
        "MerchantOrderId"=>"2014113245231706",
        "Customer" => array(
            "Name" => "Comprador rec programada"
            ),
        "Payment" => array(
            "Type" => "CreditCard",
            "Amount" => 1500,
            "Installments" => 1,
            "SoftDescriptor" => "Assinatura Fraldas"  
            )   
        )
    );


$headers = array(
    'Content-Type: application/json',
    'MerchantId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
    'MerchantKey: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
    'RequestId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx'
    );

$ch = curl_init($json_url);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $json_string
    );

curl_setopt_array( $ch, $options ); $result = curl_exec($ch); 
print_r($result);
}
subscribe()

答案 1 :(得分:0)

你会得到什么样的HTTP状态代码会很有趣:

print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE));