【问题标题】:Display content of JSON data page with SSL使用 SSL 显示 JSON 数据页面的内容
【发布时间】:2016-09-02 16:04:06
【问题描述】:

我有一个包含 JSON 多数据的 SSL 页面。

我想使用 get_content 并将每个数据解码为我的 JSON 数据的范围。

我正在使用此代码:

$url = 'https://www.myurl.....';
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, true);

foreach($data as $page) {
     if (strstr($page['key'],'v2-')){ 
        echo "<section>";
        echo $page['content'];
        echo "</section>";
    }
}

问题是,我收到了这个错误:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in /home/vivreune/www/metairie/home.php on line 86

Warning: file_get_contents(): Failed to enable crypto in /home/vivreune/www/metairie/home.php on line 86

Warning: file_get_contents(https://www.myurl.....): failed to open stream: operation failed in /home/vivreune/www/metairie/home.php on line 86

Warning: Invalid argument supplied for foreach() in /home/vivreune/www/metairie/home.php on line 89

我试图将我的代码更改为:

function getSSLPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSLVERSION,3); 
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

echo getSSLPage("https://www.myurl.....");

我也试过了:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

都没有给出任何好的结果。

接下来我做:

<?php

        $streamContextOptions=array(
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ),
        );

        $url = "https://www.myurl...";
        $jsonData = file_get_contents($url, false, stream_context_create($streamContextOptions));
        $data = json_decode($jsonData, true);
        $value = "v2-";

        foreach($data as $key => $value) {
                echo "<section>";
                echo $data['content'];
                echo "</section>";

        }
?>

我有:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in /home/vivreune/www/metairie/home.php on line 41

Warning: file_get_contents(): Failed to enable crypto in /home/vivreune/www/metairie/home.php on line 41

Warning: file_get_contents(https://www.myurl...): failed to open stream: operation failed in /home/vivreune/www/metairie/home.php on line 41

Warning: Invalid argument supplied for foreach() in /home/vivreune/www/metairie/home.php on line 45

新步骤:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION,3); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, 'https://www.myurl');
$result = curl_exec($ch);
curl_close($ch);

    $data = json_decode($result, true);
    $value = "v2-";

    foreach($data as $key => $value) {
            echo "<section>";
            echo $data['content'];
            echo "</section>";

    }

有答案:

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/vivreune/www/metairie-paulhac/bl-themes/Metairie-V2/php/home.php</b> on line <b>55</b>

2016 年 9 月 12 日的步骤: 我的 htaccess :

# Enable rewrite rules
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^muyrl\.fr$ [NC]
RewriteRule ^(.*) https://www.myurl.fr/$1 [QSA,L,R=301]
Header set Strict-Transport-Security "max-age=15811200" env=HTTPS


  $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.metairie-paulhac.fr/api/show/page/accueil');

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: <br/>' . curl_error($ch); };
curl_close($ch);

echo $result;

做:

    Curl error: 
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

【问题讨论】:

  • 除了您的其他选项外,还尝试将 CURLOPT_SSL_VERIFYHOST 设置为 false。
  • 我在哪里做的?,试了试
  • curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  • 另外,您使用的是什么版本的 SSL? file_get_contents() 当前不支持 SSL3,这意味着您必须使用 cURL 实现。
  • 注册流套接字传输 tcp、udp、unix、udg、ssl、sslv3、sslv2、tls、tlsv1.0

标签: php json ssl curl


【解决方案1】:

您的证书可能未经 CA 验证。请尝试以下方法,看看是否有效。

$streamContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);

$url = "https://www.example.com/somepage";
$jsonData = file_get_contents($url, false, stream_context_create($streamContextOptions));
$data = json_decode($jsonData, true);

顺便说一句,您可以使用foreach($data as $key =&gt; $value) 来返回键和值,而不是每次都使用strstr() 方法检查。

【讨论】:

  • 我的回答结果:
  • strstr() 是不显示某些页面。
  • 我知道,所以检查 $key 而不是使用 strstr()。
【解决方案2】:

我不确定您的数据结构看起来如何,所以我将使用您以前版本的 foreach 方法。至少,按照我在您问题的 cmets 中推荐的使用 cURL 似乎已经摆脱了另一个错误。因此,请尝试以下操作。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION,3); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, 'https://www.myurl');
$result = curl_exec($ch);
curl_close($ch);

$data = json_decode($result, true);
foreach($data as $page) {
    if (strstr($page['key'],'v2-')){ 
        echo "<section>";
        echo $page['content'];
        echo "</section>";
    }
}

【讨论】:

  • 相同:警告:在 /home/vivreune/www/metairie/home.php 中为 /home/vivreune/www/metairie/home.php 提供的 foreach() 参数无效>53
  • 我会在你的高音扬声器上给你我的数据源
  • 这意味着$data 不是您所期望的。实际上,在这种情况下,$result 要么为 True,要么为 False,因为 CURLOPT_RETURNTRANSFER 未设置为 true。很抱歉误导您,但我这样做是为了让您了解 curl_exec()。根据文档,curl_exec() 在成功时返回 TRUE,在失败时返回 FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER 选项,它将在成功时返回结果,在失败时返回 FALSE。
  • ok ... 所以,如果 CURLOPT_RETURNTRANSFER 未设置或设置为 false 且设置为数据,则我的 $data 将没有任何结果。对不起,我是法国人,我尽量理解
  • 是的,如果 CURLOPT_RETURNTRANSFER 未设置为 True,$data 将是一个布尔值。
【解决方案3】:

所以,终于找到了,太简单了……

$url = 'http://www.myurl:443/myapipages';
$result = file_get_contents($url);
    $data = json_decode($result, true);
    foreach($data as $page) {
        if (strstr($page['key'],'v2-')){ 
            echo "<section>";
            echo $page['content'];
            echo "</section>";
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2011-01-09
    • 2011-04-20
    • 2023-03-22
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多