douqiao7188 2014-06-26 08:23
浏览 188

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

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)); 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 )

The request is generated and I receive HTTP 200, OK status as response. 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.

  • 写回答

1条回答 默认 最新

  • dongpo1216 2014-06-26 08:32
    关注

    1) CURLOPT_POST option expect value of boolean type. However it doesn't matter in your code since count($fields) will be casted to true if $fields is non-empty.

    2) Don't build POST data manually, use http_build_query() instead. Also you can pass array of data to CURLOPT_POSTFIELDS option and will convert it to string automatically .

    3) You don't need to set Content-Type header since cURL will set it automatically for POST request.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办