【问题标题】:PHP file_get_contents changing apersand in URLPHP file_get_contents 更改 URL 中的和号
【发布时间】:2017-09-25 14:13:54
【问题描述】:
$Link = 'http://somewebsite.com/api.php?message=someMessage&user_id=' . $UserID . '&password=' . $UserPW;
echo $Link;
echo "<br><br>";

$PageResponse = file_get_contents($Link);

出于隐私考虑,我无法发布工作示例,但这是我遇到的问题。

当我回显 $Link 时,它会返回:

 http://somewebsite.com/api.php?message=someMessage&user_id=redacted&password=redacted

但是 file_get_contents 返回和错误如下所示:

 Warning: file_get_contents(http://somewebsite.com/api.php?message=someMessage&amp;user_id=redacted&amp;password=redacted): failed to open stream: No connection could be made because the target machine actively refused it. in C:\wamp64\www\hello.php on line 14

如果您注意到,它会将“&”字符更改为&amp;amp;,这会破坏 url。有没有简单的方法来解决这个问题?

【问题讨论】:

  • 这不是错误,这是一个特性
  • 在使用file_get_contents 之前,您是否尝试过html_entity_decode url 字符串?否则str_replace 会有所帮助。
  • 已尝试解码和替换。转换仍然存在

标签: php file-get-contents


【解决方案1】:

您可以创建一个数据数组。比序列化它以使其成为字符串。 现在我们应该使用 openssl_encrypt 和 openssl_decrypt。

你可以这样做:

    $password          = "EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va";
$data              = [
    'message'  => 'someMessage',
    'user_id'  => '111',
    'password' => 'paaa'
];
$string_to_encrypt = serialize( $data );

$encrypted_string = openssl_encrypt( $string_to_encrypt, "AES-128-ECB", $password );
//print_r($encrypted_string);
$_SESSION["encrypted_value"] = $encrypted_string;

现在您的加密数据已在会话中,因此您可以从您的站点的任何地方获取它。 现在像这样获取您的数据

 $encrypted_value = $_SESSION["encrypted_value"];
$decrypted_string=openssl_decrypt($encrypted_value,"AES-128-ECB",$password);
//print_r($decrypted_string);
$result = unserialize($decrypted_string);
print_r($result);

最后你会得到一个数组。

您必须开始您的会话

session_start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 2013-05-10
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    相关资源
    最近更新 更多