如何在PHP中读取字符串变量$ _GET或$ _POST?

时间:2017-10-20 07:12:42

标签: php

如何将字符串作为查询读取,就像转到https://www.youtube.com/get_vide_info?v= {{video_id}}

一样

它将提供一个信息作为查询(如果你把它放在$ _GET中),PHP将手动读取它!

如果没有在$ _GET中传递它,如果有一个函数

,我怎么能读它

示例:

<?php


$s = file_get_contents('https://www.youtube.com/get_vide_info?v={{video_id}}');

// for example it returned status=failed&error=404&reason=copyright

//how can read status and error and reason

// for example 
echo "reject reason $s->reason ";
echo "status $s->status ";

2 个答案:

答案 0 :(得分:0)

一个简单的选择是使用parse_str(),你可以将其作为数组访问,如果你想作为对象,你必须将它转换为对象。

// arg1 => query string
// arg2 => output array
parse_str($your_string_response, $output);

echo $output['reason'];
echo $output['status'];

测试结果:

$ cat test.php 
<?php

$string = "status=failed&error=404&reason=copyright";
parse_str($string, $output);
print_r($output); // as array
print_r((object)$output); // as object

?>

输出:

$ php test.php 
Array
(
    [status] => failed
    [error] => 404
    [reason] => copyright
)
stdClass Object
(
    [status] => failed
    [error] => 404
    [reason] => copyright
)

答案 1 :(得分:0)

你可以用魔术字段$ _GET ['NameOfQuery']读取queryString;

$s=array("status"=>$_GET['status'],
"error"=>$_GET['error'],
"reason"=>$_GET['reason']);

// out
echo "reject reason:".$s['reason'];
echo "status :".$s['status'];
echo "error :".$s['error'];;