2017-01-18 4 views
1

나는 이것이 일반적인 질문이라는 것을 알고 있습니다. PHP에서 url로부터 JSON을 가져 오는 방법을 알고 싶습니다.URL에서 PHP를 가져와

은 .PHP 파일은 아래 파일이된다 :

<html> 
 
<head> 
 
    <?php 
 
    // $url = "https://localhost:8666/web1/popupData/dataWeekly.php"; 
 
    // $emp = file_get_contents($url); 
 

 
    $json_url = "https://localhost:8666/web1/popupData/dataWeekly.php"; 
 
    $json = file_get_contents($json_url); 
 
    $emp = json_decode($json, TRUE); 
 

 

 

 
    ?> 
 
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 
 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script> 
 

 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css"> 
 

 
    <script type="text/javascript"> 
 
    $(document).ready(function(){ 
 
     var JSON = <?php echo json_encode($emp); ?>; 
 

 
     $('.details').click(function(){ 
 
      var name = $(this).attr('id'); 
 

 
      $('#dialog').html('<p>Name : ' + name + '</p> <p>Dept : ' + JSON[name]['dept'] + '</p> <p>Age : ' + JSON[name]['age'] + '</p>'); 
 
      $('#dialog').dialog(); 
 
     }); 
 
    }) 
 
    </script> 
 
</head> 
 
<body> 
 

 
<!-- Dialog Box --> 
 
<div style="display: none;" id="dialog"></div> 
 
<!-- Dialog Box --> 
 

 
<?php 
 
    foreach($emp as $key => $record) 
 
    { 
 
    ?> 
 
     <ul id="<?php echo $key ?>" class="details"> 
 
      <li>Name: <?php echo $key ?></li> 
 
      <li>Dept: <?php echo $record['dept'] ?></li> 
 
     </ul> 
 
    <?php 
 
    } 
 
?> 
 

 
</body> 
 
</html>

이 내가 위의 PHP 파일 실행으로 내가 점점 오전 현재 오류입니다 :

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\xampp\htdocs\web1\popupData\mp.php on line 8 
 

 
Warning: file_get_contents(): Failed to enable crypto in C:\xampp\htdocs\web1\popupData\mp.php on line 8 
 

 
Warning: file_get_contents(https://localhost:8666/web1/popupData/dataWeekly.php): failed to open stream: operation failed in C:\xampp\htdocs\web1\popupData\mp.php on line 8 
 

 
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\web1\popupData\mp.php on line 39

도와주세요. 당신은 당신이 이 간단 GET에 대한 위대한 & 시스템에 allow_url_fopen을를 활성화해야합니다 file_get_contents()를 사용하는 경우

+0

가능한 복제를 사용해보십시오 http://stackoverflow.com/questions/14078182/openssl-file-get 브라우저에서 URL을 여는처럼 HTTP 요청을 곱슬 곱슬 -contents-failed-to-enable-crypto – xReprisal

+0

힌트를 보내 주셔서 감사합니다. @ xReprisal – jane

+0

[OPENSSL 파일 \ _get \ _contents()의 가능한 복제본 : 암호화를 사용하지 못했습니다] (http://stackoverflow.com/questions/14078182/) openssl-file-get-contents-failed-to-enable-crypto) –

답변

2

안녕 당신도 cURL 또는 file_get_contents()

하지만 URL

에서 데이터를로드하는 데 사용할 수있는 요청

cURL

$json_url = "https://localhost:8666/web1/popupData/dataWeekly.php"; 
$crl = curl_init(); 
curl_setopt($crl, CURLOPT_URL, $json_url); 
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, FALSE); 
$json = curl_exec($crl); 
curl_close($crl); 
$emp = json_decode($json, TRUE); 
+0

고마워요. @msk – jane

+0

잘 작동합니까? – msk

+0

예 ... 그렇습니다 !! – jane

관련 문제