file_get_contents()函数进行身份验证时返回一个空字符串,否则罚款 [英] file_get_contents() returns an empty string when authenticating, otherwise fine

查看:136
本文介绍了file_get_contents()函数进行身份验证时返回一个空字符串,否则罚款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是新来与API和PHP的工作,所以大家多多包涵。我想打一个API认证到通过GET参数网站(如API文档中的建议)。我尝试使用file_get_contents()函数返回它返回的身份验证令牌。一个重定向到URL输出令牌XML格式。

So, I'm new to working with API's and PHP, so bear with me. I'm trying to hit an API to authenticate into the site via GET parameters (as suggested in the API documentation). I'm trying to use file_get_contents() to return the authentication token it returns. A redirect to the URL outputs the token in XML format.

然而,当我这样做时,它返回一个空字符串。我怎样才能返回完整的XML输出​​?

However, when I do the following, it returns an empty string. How can I return the full XML output?

$token = file_get_contents('http://example.com/api.asp?cmd=logon&email=xxx@something.com&password=s0mep@SSword');
var_dump($token);

我得到以下的输出:

I get the following output:

string(116) ""

关于我做错了任何想法?

Any ideas about what I'm doing wrong?

推荐答案

尝试这样做:

$token = str_replace("<", "&lt;", $token);
$token = str_replace(">", "&gt;", $token);

vd($token);

这是因为发生您的浏览器间preting的XML你正在阅读HTML和读取XML的标记。 (你可以告诉该字符串不是空的,因为 VD ,这我假设是的var_dump 的别名告诉你字符串的长度 116 )更换&LT; &GT; 的与有效的HTML实体应该解决这个问题。

This is occurring because your browser is interpreting the XML you're reading as HTML and reading the XML as tags. (You can tell the string is not empty because vd, which I'm assuming is an alias for var_dump tells you the string's length is 116.) Replacing the <'s and >'s with their valid HTML entities should resolve that.

这篇关于file_get_contents()函数进行身份验证时返回一个空字符串,否则罚款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆