【问题标题】:Requesting Api Call using cURL使用 cURL 请求 API 调用
【发布时间】:2017-02-22 14:57:25
【问题描述】:

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

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);                

}

找到网站说明的链接:

【问题讨论】:

  • 您应该调试您的 curl 请求...查看 api 文档中的请求标头,您必须发送更多标头

标签: php json api class curl


【解决方案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()

【讨论】:

    【解决方案2】:

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

    print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE));
    

    【讨论】:

    • 你得到 101 因为你没有发送所有需要的头做 api
    • @SimonMüller 可能有点太快太粗心了 :-) 你的回答很好。
    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多