如何发送get请求并在php中接收json数据

时间:2017-01-22 16:10:50

标签: javascript php json

我其实想知道如何在php中创建这个请求

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":".."}}

非常感谢你的帮助

1 个答案:

答案 0 :(得分:1)

您可以使用curl正常发送请求。要传递标题,请使用以下示例:

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