简体   繁体   English

PHP curl请求奇怪的参数

[英]PHP curl request for weird parameters

I'm trying to automate signing up for classes (cause im allways forgetting to do it) When i manually sign up it uses this url for classes on a specific day: 我正在尝试自动注册课程(因为我总是忘记这样做)当我手动注册时,它会在特定日期使用此URL进行课程:

https://URL.com/public/tickets.php?PRESET%5BTickets%5D%5Bname%5D%5B%5D=&PRESET%5BTickets%5D%5Bday%5D%5B%5D=2018-03-04

which decodes into 它解码成了

https://URL.com/public/tickets.php?PRESET[Tickets][name][]=&PRESET[Tickets][day][]=2018-03-04

But im having the hardest time translating this into a curl request. 但我正在努力将其转化为卷曲请求。 I've (amongst other things) tried 我(除其他外)尝试过

$data = array("PRESET" => array("Tickets" => array("name"=>array(""), "day"=> array("2018-03-02"))));

and

$data = array('PRESET[Tickets][naam][]=', 'PRESET[Tickets][naam][]=');

But i allways get a page where no day has been selected. 但我总是得到一个没有选择日期的页面。 Sometimes there is a php error on the page about a parameter that is expected to be an array. 有时在页面上有一个关于参数的php错误,该参数应该是一个数组。

this is my curl request 这是我的卷曲请求

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $targetSite);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

Can someone tell me how to properly send the parameters with the curl request? 有人能告诉我如何使用curl请求正确发送参数吗? Thanks! 谢谢!

when you put it into CURLOPT_POSTFIELDS, you're putting it into the request body, not the request URL. 当你把它放入CURLOPT_POSTFIELDS时,你将它放入请求体,而不是请求URL。 furthermore, when you give CURLOPT_POSTFIELDS an array, curl will encode it in multipart/form-data -format, but you need it urlencoded (which is different from multipart/form-data). 此外,当您为CURLOPT_POSTFIELDS提供一个数组时,curl将以multipart/form-data -format对其进行编码,但您需要urlencoded(与multipart / form-data不同)。 remove all the POST code, and use http_build_query to build the url, 删除所有POST代码,并使用http_build_query构建网址,

curl_setopt ( $ch, CURLOPT_URL, "https://URL.com/public/tickets.php?" . http_build_query ( array (
        'PRESET' => array (
                'Tickets' => array (
                        'name' => array (
                                0 => '' 
                        ),
                        'day' => array (
                                0 => '2018-03-04' 
                        ) 
                ) 
        ) 
) ) );

and protip, you can use parse_str() to decode urls into php arrays, and furthermore, you can use var_export to get valid php code to create that array at runtime, and finally, as shown above, you can use http_build_query to convert that array back to an url, that's what i did here. 和protip一样,你可以使用parse_str()将url解码成php数组,而且,你可以使用var_export来获取有效的php代码来在运行时创建该数组,最后,如上所示,你可以使用http_build_query来转换那个数组回到网址, 这就是我在这里所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM