拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 通行证使用多物件阵列curlphp

通行证使用多物件阵列curlphp

白鹭 - 2022-01-24 2101 0 0

我可以知道是正确的方法,通过 curl PHP

$data6 = array (
"CorrelationId"  => 'CorrelationId',
"ConfirmationId"  => 'ConfirmationId',
"Contact.Title"  => 'Miss',
"Contact.FirstName"  => 'FirstName',
"Contact.LastName"  => 'LastName',
"Contact.MobilePhone"  => '1234567',
"Contact.HomePhone"  => '12356778',
"Contact.Email"  => 'admin@gmail.com',
"Contact.Remark"  => '',   
"Guests[0]Index"  => 1,
"Guests[0]Title" => 'Miss',
"Guests[0]FirstName" => 'FirstName',
"Guests[0]LastName" => 'LastName',
"Guests[0]MobilePhone" => '123456',
"Guests[0]HomePhone" => ' 123456677',
"Guests[0]Email" => 'admin@gmail.com',
"Guests[0]Type" => 1,
"Guests[0]Age" => 21,
"Beds" => '',); 

下面是原始JSON传递资料,原始JSON资料来自邮递员,我运行邮递员很好。我认为我的错误是我的代码传递自变量。

{
"CorrelationId": "CorrelationId",
"ConfirmationId": "ConfirmationId",
"Contact": {
    "Title": "Miss",
    "FirstName": "FirstName",
    "LastName": "LastName",
    "MobilePhone": "1234567",
    "HomePhone": " 6287717564805",
    "Email": "admin@gmail.com",
    "Remark": ""
},
"Guests": [
    {
        "Index": 1,
        "Title": "Miss",
        "FirstName": "FirstName",
        "LastName": "LastName",
        "MobilePhone": "1234567",
        "HomePhone": " 6287717564805",
        "Email": "admin@gmail.com",
        "Type": 1,
        "Age": 21
    }
],
"Beds": []

}

uj5u.com热心网友回复:

是的,marliah,您可以按如下方式传递您的阵列。我建议没有必要为客人添加额外的括号。如果这样做,则必须使用索引 0 来访问资料。喜欢Guests[0][Age]

<?php

$data = array(
    "CorrelationId" => "CorrelationId",
    "ConfirmationId" => "ConfirmationId",
    "Contact" => [
        "Title" => "Miss",
        "FirstName" => "FirstName",
        "LastName" => "LastName",
        "MobilePhone" => "1234567",
        "HomePhone" => " 6287717564805",
        "Email" => "admin@gmail.com",
        "Remark" => ""
    ],
    "Guests" => [[

        "Index" => 1,
        "Title" => "Miss",
        "FirstName" => "FirstName",
        "LastName" => "LastName",
        "MobilePhone" => "1234567",
        "HomePhone" => " 6287717564805",
        "Email" => "admin@gmail.com",
        "Type" => 1,
        "Age" => 21

    ]],
    "Beds" => []
);

print_r(json_encode($data));

您可以使用以下函式将阵列传递到后端。不要忘记使用json_encode().

call('POST','API_URL',json_encode($data),'USERNAME','PASSWORD');

如果您在后端没有任何身份验证设定,请忽略$username$password. 洗掉此行以忽略身份验证凭据。

curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);

.

public function call($method, $url, $data, $username, $password)
{
    $curl = curl_init();
    switch ($method) {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
    // OPTIONS:
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "Accept: application/json",
    ));
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    //Check for errors.
    if (curl_errno($curl)) {
        //If an error occured, throw an Exception.
        throw new Exception(curl_error($curl));
    }
    // EXECUTE:
    $result = curl_exec($curl);
    if (!$result) {
        die("Connection Failures");
    }
    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    echo 'HTTP status: ' . $httpcode;
    return $result;
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *