CURL 中的 data-urlencode 是什么意思?

Jes*_*sen 6 php curl

我已经搜索了很多小时,试图弄清楚 php curl 中的 --data-urlencode 是什么。

我试过这个,但我不认为它是正确的。

$xmlpost .= "object1[file]=@https://www.lob.com/goblue.pdf";
Run Code Online (Sandbox Code Playgroud)

在文档中它是:

--data-urlencode "object1[file]=https://xxx.pdf" \
Run Code Online (Sandbox Code Playgroud)

但是在 CURL 中它是什么? curl_setopt($cpt, CURLOPT_??)

完整的 API DOCS 说我需要调用它

curl https://api.lob.com/v1/jobs \
-u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc: \
-d "name=Demo Quick Print Job" \
-d "to[name]=Harry Zhang" \
-d "to[address_line1]=123 Test Street" \
-d "to[address_city]=Mountain View" \
-d "to[address_state]=CA" \
-d "to[address_zip]=94085" \
-d "to[address_country]=US" \
-d "from[name]=Leore Avidar" \
-d "from[address_line1]=123 Test Street." \
-d "from[address_line2]=Apt 155" \
-d "from[address_city]=Sunnyvale" \
-d "from[address_state]=CA" \
-d "from[address_zip]=94085" \
-d "from[address_country]=US" \
-d "object1[name]=testobject" \
--data-urlencode "object1[file]=https://www.lob.com/goblue.pdf" \
-d "object1[setting_id]=100"
Run Code Online (Sandbox Code Playgroud)

因此,我正在尝试:

<?php
 $xmlpost = array(
    "name" => "PostalAddressConfirmation",
    "to" => array(
        "name" => "Testing",
        "address_line1" => "testvej",
        "address_city" => "test",
        "address_state" => "test",
        "address_zip" => "5000",
        "address_country" => "DK"
    ),
    "from" => array(
        "name" => "test ApS",
        "address_line1" => "testvej 25",
        "address_city" => "test",
        "address_state" => "test",
        "address_zip" => "5000",
        "address_country" => "DK"
    ),
    "object1" => array(
        "name" => "test Object",
        "setting_id" => "100",
        "file" => "https://lob.com/goblue.pdf"
    )
);

$cpt = curl_init("https://api.lob.com/v1/job");
curl_setopt_array($cpt, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POSTFIELDS => $xmlpost,
    CURLOPT_USERPWD => 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:',
));

$result = curl_exec($cpt);
Run Code Online (Sandbox Code Playgroud)

Édo*_*pez 6

从你得到的手册页

--data-urlencode <data>

(HTTP) 这会发布数据,类似于其他 --data 选项,不同之处在于它执行 URL 编码。

有关信息,该--data选项描述如下:

-d, --data <data>

这将导致 curl 使用 content-type 将数据传递给服务器application/x-www-form-urlencoded


Asa*_*nka 5

您发送到服务器的数据必须已经正确编码,curl 不会为您执行此操作。例如,如果您希望数据包含空格,则需要将该空格替换为 %20 等。

 curl --data "birthyear=1905&press=%20OK%20"
Run Code Online (Sandbox Code Playgroud)

如果不遵守这一点,很可能会导致您的数据被错误接收并弄乱。

最近的curl版本实际上可以为您对POST数据进行url编码,如下所示:

 curl --data-urlencode "name=I am Daniel" 
Run Code Online (Sandbox Code Playgroud)

参考: https: //curl.haxx.se/docs/httpscripting.html


Ja͢*_*͢ck 3

答案是你甚至不需要考虑它;你甚至不需要考虑它。考虑以下代码:

$xmlpost  = [
    "name" => "PostalAddressConfirmation",

    // Lav modtager
    "to" => [
        "name" => "Jespern",
        "address_line1" => "hejvej",
        "address_city" => "Odense",
        "address_state" => "Syddanmark",
        // etc ...
    ],

    // Lav afsender
    "from" => [
        "name" => "test",
        // etc.
    ],

    //Dokument der skal sendes
    "object1" => [
        "name" => "AddressConfirmation",
        "setting_id" => 100,
        "file" => "https://www.lob.com/goblue.pdf",
        "quantity" => 1,
    ],
];

$cpt = curl_init("https://api.lob.com/v1/job");
curl_setopt_array($cpt, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_USERPWD => 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:',
    CURLOPT_POSTFIELDS => http_build_query($xmlpost),
]);

$result = curl_exec($cpt);
Run Code Online (Sandbox Code Playgroud)

http_build_query()函数将构建适当的字符串来发布。