用php解析html

用php解析html,php,Php,在使用FacebookConnect时,我必须从url中检索访问令牌(它不在url中,而是在该url的文件中),所以这就是我要做的 $url = "https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&redirect_uri=http://www.example.com/facebook/oauth/&client_secret=".$facebook_secret."&

在使用FacebookConnect时,我必须从url中检索访问令牌(它不在url中,而是在该url的文件中),所以这就是我要做的

$url = "https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&redirect_uri=http://www.example.com/facebook/oauth/&client_secret=".$facebook_secret."&code=".$code;"

function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 


$access_token = get_string_between(file_get_contents($url), "access_token=", "&expires=");  

它看起来又丑又笨拙有更好的方法吗?谢谢。

您可以使用PHP函数
parse\u str

"https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&redirect_uri=http://www.example.com/facebook/oauth/&client_secret=".$facebook_secret."&code=".$code;

parse_str(file_get_contents($url));

echo $access_token;
从PHP手册:

void parse_str(字符串$str[,数组和$arr])

解析str,就像它是查询一样 通过URL传递的字符串,并设置 当前范围中的变量