如何将变量放入curl中

时间:2019-05-11 03:52:59

标签: php curl

(我谨向在地毯上的英语致歉)

我想通过curl发送请求,我使用正常的代码。当我更改将变量添加到数组中的代码时,代码可以正常工作,也不要设置此Cookie。

我尝试过设置:

'Cookie: ssid=.$input; path=/; domain=example.com;',

'Cookie: "ssid=".$input; path=/; domain=example.com;',

'Cookie: 'ssid='.$input; path=/; domain=example.com;',

这就是全部代码

$ch4=curl_init("https://example.com/");
curl_setopt_array($ch4,array(
        CURLOPT_USERAGENT=>' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
        CURLOPT_ENCODING=>'gzip, deflate',
        CURLOPT_HTTPHEADER=>array(
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language: en-US,en;q=0.5',
                'Accept-Encoding: gzip, deflate',
                'Connection: keep-alive',
                'Upgrade-Insecure-Requests: 1',
                'Cookie: ssid=.$input; path=/; domain=example.com;',
        ),
));

我希望从变量($ input)中读取该蛋糕

2 个答案:

答案 0 :(得分:0)

将变量分配给特定数组

$myArr['message']['to_recipients'] = $emails;

然后设置卷曲

$ch = curl_init();
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $myArr ));

答案 1 :(得分:0)

您可以手动将cookie与文件中的cookie一起发送(使用cookie-file选项)。例如:

//SEND DATA VARIABLE IN JSON ENCODED FORMAT
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $myArr ));

// sending manually set cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie"));

// sending cookies from file
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

对于您的代码,您已经在cookie中使用了php变量作为字符串,因此您必须像下面给出的那样更新脚本。

$ch4=curl_init("https://example.com/");
curl_setopt_array($ch4,array(
        CURLOPT_USERAGENT=>' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
        CURLOPT_ENCODING=>'gzip, deflate',
        CURLOPT_HTTPHEADER=>array(
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language: en-US,en;q=0.5',
                'Accept-Encoding: gzip, deflate',
                'Connection: keep-alive',
                'Upgrade-Insecure-Requests: 1',
                'Cookie: ssid='.$input.'; path=/; domain=example.com;',
        ),
));