简体   繁体   English

使用自定义标头(POST)发出请求

[英]Make request with custom headers (POST)

My code isn't working, tried a few things but I'm new to php so yeah... here's what I got, always returns me a blank page. 我的代码无法正常工作,尝试了一些操作,但是我对php还是陌生的,所以...这就是我得到的,总是让我返回空白页。

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$rnd = $_GET['rnd'];
$ch = curl_init("http://chat.website.com/script/login.php?rnd=".$rnd);
$request_headers = array();
$request_header[] = (
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'onLoad: [type Function]',
'p: password',
'u: username',
'owner: [object Object]
');
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$userdata = curl_exec($ch);
echo $userdata;
?>

you are passing $request_headers but the data you have in $request_header and better see your array is fine. 您正在传递$request_headers但您在$request_header拥有的数据又更好地看到了数组。

or may be try something like this: 或者可以尝试这样的事情:

$request_header[] = array('User-Agent'=>'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36',
'Content-Type'=> 'application/x-www-form-urlencoded',
'onLoad'=>'[type Function]',
'p'=>'username',
'u'=>'password',
'owner'=>'[object Object]
');

I found my error, I wasn't making the request in POST. 我发现了自己的错误,我没有在POST中发出请求。 Here's the code that is working if anyone needs it: 如果有人需要,下面的代码可以正常工作:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$rnd = 1;
$rnd = $_GET['rnd'];
$ch = curl_init("http://chat.website.com/scripts/login.php?rnd=".$rnd);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "onLoad=%5Btype%20Function%5D&p=password&u=username&owner=%5Bobject%20Object%5D");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$userdata = curl_exec($ch);
echo $userdata;
?>

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

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