2017-01-22 62 views
1

其实我是想知道如何在PHP中创建这个请求如何发送GET请求,并在PHP收到JSON数据

GET /current_user HTTP/1.1 
Host: localhost 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Referer: http://burp/show/1 
X-Requested-With: XMLHttpRequest 
Cookie: ... 
Connection: close 
Upgrade-Insecure-Requests: 1 

这是非常重要的添加行

X-Requested-With: XMLHttpRequest 

在要求,也是我需要得到JSON响应是这样

{"csrf_token":"..............","signed_in?":true,"disclosure_directory_submissions_enabled":false,"new_feature_article_available":false,"bounty_statistics_enabled":true,"pro_community_enabled":false,"davr_enabled":false,"is_member_of_teams":false,"can_request_endorsements":false,"whitelisted_team_ids":[],"edit_unclaimed_profiles":false,"signal":null,"email":"........","id":.....,"username":"....","name":"Mohamed Sherif","bio":"","url":".","profile_picture_urls":{"small":"..","medium":".."}} 

非常感谢您的帮助

+0

如果您有它在服务器上,使用卷曲:http://php.net/manual/en/intro.curl.php – Giladd

+1

您不能创建一个' XMLHttpRequest'在PHP中。您可以从客户端生成XHR请求,而不是服务器。 –

+0

能否请您进一步解释着我,甚至加入这一行的JavaScript或东西 –

回答

1

您可以使用卷曲正常发送请求。对于经过头,使用下面的例子:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"http://localhost/current_user"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to get return value from server 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With: XMLHttpRequest' 
)); 
$output = curl_exec ($ch); 
curl_close ($ch); 
echo $output;