简体   繁体   English

发布/获取请求未发送参数

[英]Post/Get Request not sending parameters

I am trying to send username and password through post request to login page on my localhost. 我正在尝试通过发布请求将用户名和密码发送到本地主机上的登录页面。 I am using below code to generate request and send it to the page 我正在使用下面的代码来生成请求并将其发送到页面

$url='http://localhost/index.php';
$fields = array('username' => 'admin', 'password' => '1234');
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

Edit 编辑
print_r(curl_getinfo($ch)); 的print_r(curl_getinfo($ CH)); prints this information 打印此信息
Array ( [url] => http://www.url.com/index.php [content_type] => text/html [http_code] => 200 [header_size] => 380 [request_size] => 182 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.234 [namelookup_time] => 0.063 [connect_time] => 0.063 [pretransfer_time] => 0.063 [size_upload] => 28 [size_download] => 2322 [speed_download] => 1039 [speed_upload] => 12 [download_content_length] => 2322 [upload_content_length] => 28 [starttransfer_time] => 2.234 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 80 [local_ip] => ::1 [local_port] => 54953 [redirect_url] => ) Array ( [Host] => localhost [Accept] => / [Content-Length] => 28 [Content-Type] => application/x-www-form-urlencoded ) 数组([url] => http://www.url.com/index.php [content_type] =>文本/ html [http_code] => 200 [header_size] => 380 [request_size] => 182 [filetime] = > -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.234 [namelookup_time] => 0.063 [connect_time] => 0.063 [pretransfer_time] => 0.063 [size_upload] => 28 [size_download] => 2322 [speed_download] => 1039 [speed_upload] => 12 [download_content_length] => 2322 [upload_content_length] => 28 [starttransfer_time] => 2.234 [redirect_time] => 0 [certinfo] =>数组()[primary_ip] => :: 1 [primary_port] => 80 [local_ip] => :: 1 [local_port] => 54953 [redirect_url] =>)数组([Host] => localhost [Accept] => / [Content-Length] => 28 [Content-Type] => application / x-www-form-urlencoded)

The request is generated and I receive HTTP 200, OK status as response. 生成了请求,并且我收到HTTP 200的OK状态作为响应。 The request is not sending any parameters with it, and in response I receive normal login form instead of after login screen. 该请求未发送任何参数,因此,我收到普通的登录表单,而不是登录屏幕后。

Anyone please tell me what is wrong with above code. 任何人都请告诉我上述代码有什么问题。

I have tried same with stream_context_create and file_get_contents but same reponse is generated in both cases(login form instead of post login screen) like they are receving any parameters. 我已经尝试过使用stream_context_create和file_get_contents进行相同操作,但是在两种情况下(生成表单而不是登录后屏幕)都生成相同的响应,就像它们正在接收任何参数一样。

1) CURLOPT_POST option expect value of boolean type. 1) CURLOPT_POST选项期望boolean类型的boolean However it doesn't matter in your code since count($fields) will be casted to true if $fields is non-empty. 但是,这在您的代码中没有关系,因为如果$fields为非空,则count($fields)将被强制转换为true

2) Don't build POST data manually, use http_build_query() instead. 2)不要手动构建POST数据,请改用http_build_query() Also you can pass array of data to CURLOPT_POSTFIELDS option and will convert it to string automatically . 您也可以将数据array传递给CURLOPT_POSTFIELDS选项,并将其自动转换为string

3) You don't need to set Content-Type header since cURL will set it automatically for POST request. 3)您不需要设置Content-Type标头,因为cURL将为POST请求自动设置它。

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

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