PHP Curl - 如何进行POST请求,参数应该是正文中的JSON格式

时间:2014-01-16 04:01:27

标签: php json curl

我需要在PHP curl中复制相同的POST请求。请求参数应该在正文中的json中,响应也是json对象。

Fiddler sample

{
"api_key": "scTrCT",
"test": "true",
"service_provider_list": [
    {
        "facility_name": "ALL YOUR SMILE NEEDS DENTAL CENTERS",
        "provider_name": "DRS. HERMAN AND MACK P.C",
        "tax_id": "12345678
    }
],
"payer_ids": [
    "00431"
],
"transaction_type": "270",
"effective_date": "2014-01-12"
}

1 个答案:

答案 0 :(得分:0)

请试试这个:

<?php

$json = array(
    "api_key" => "scTrCT",
    "test" => "true",
    "service_provider_list" => array(
        "facility_name" => "ALL YOUR SMILE NEEDS DENTAL CENTERS",
        "provider_name" => "DRS. HERMAN AND MACK P.C",
        "tax_id" => "12345678"
    ),
    "payer_ids" => array(
        "00431"
    ),
    "transaction_type" => "270",
    "effective_date" => "2014-01-12"
);

$json = json_encode($json);

$ch = curl_init('http://gds.eligibleapi.com/v1.3/enrollment.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

echo $result;