[英]PHP curl POST request with JSON response
我正在尝试发送帖子数据,并从api(qbittorrent)接收SID,以便稍后发送get请求。 我目前无法确定,但是json为NULL。 这是我的代码:
<?php
$ch = curl_init();
$fields = array( 'username'=>'Admin','password'=>'mypassword');
$postvars = '';
foreach($fields as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$postvars = rtrim($postvars, '&');
$url = "https://192.123.123.123:8080/login";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
$headers= array('Accept: application/json','application/x-www-form-urlencoded');
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
var_dump(json_decode($response, true));
print "curl response is:" . $response;
curl_close ($ch);
?>
您可以在这里https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-Documentation#authorization查看API文档
我能够使用powershell来获取json,就像这样:
$prms = @{username="Admin";password="mypassword"}
$res = Invoke-WebRequest https://192.123.123.123:8080/login -Method POST -Body prms -UseBasicParsing
但是我无法用php做到这一点! 如果您还想使用SID作为cookie来引导我正确处理get请求,我将不胜感激:)
问题就在那儿,改变了:
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
至
curl_setopt($ch, CURLOPT_HEADER, true);
然后我得到了json响应!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.