我已经在该网站上进行了广泛搜索,但我试图用从表单中插入的数据替换Curl请求中的参数,只是语法不正确。
此代码有效:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",\"params\":[\"Device\"],\"chain_name\":\"nw-504-042-6\"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Content-Type: application/json",
"Postman-Token: dd629e44-26fb-4ed1-ac4e-c47faf38c356",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
此代码无法在CURLOPT_POSTFIELDS中用变量$ ChainName替换“ nw-504-042-6 \”。
<?php
$ChainName = $_POST['ChainName'];
$StreamName = $_POST['StreamName'];
echo $ChainName;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8000",
CURLOPT_URL => "http://nd-337-495-552.rg-709-313.p2pify.com:8000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\"method\":\"liststreamitems\",
\"params\":[\"Device\"],
\"chain_name\":"'"$ChainName"'"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefghijklmnop1234567=",
"Postman-Token: 0bcbbec4-cc25-4059-b2c4-220c4722b9d5",
"cache-control: no-cache"
), // auth header content deliberately changed
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
非常感谢您的帮助
答案 0 :(得分:2)
尝试json_encode
您的参数
$params = [
"method"=>"liststreamitems",
"params"=>["Device"],
"chain_name"=>$ChainName
];
然后将其传递给选项CURLOPT_POSTFIELDS
CURLOPT_POSTFIELDS => json_encode($params);