使用SSL

时间:2016-09-02 16:04:06

标签: php json ssl curl

我有一个包含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年12月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

3 个答案:

答案 0 :(得分:0)

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 => $value)来回复键和值,而不是每次都使用strstr()方法进行检查。

答案 1 :(得分:0)

我不确定您的数据结构如何,所以我将使用您之前版本的foreach方法。至少,正如我在你的问题的评论中所推荐的那样使用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>";
    }
}

答案 2 :(得分:0)

所以,最后我发现它,它很容易......

$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>";
        }
    }