api 하나 질문드리겠습니다.

api 하나 질문드리겠습니다.

QA

api 하나 질문드리겠습니다.

본문

1028819893_1691072038.552.png

 

 

index.php 에 저 빨간 동그라미 숫자만 불러오게 하려고 합니다.

 


  $api_url = 'http://도메인/map_data_i3.php?id=2387';
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $api_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_GET, true);
  curl_setopt($ch, CURLOPT_HEADER, false);
  $response = curl_exec($ch);
  $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  
  echo json_encode($tree_data, true);

 

이런식으로 했는데 맞는지 모르겠어요 ㅠㅠ 우선 정상으로 불러오질 않습니다. ㅠㅠ null 값 나오네요 ㅠ

이 질문에 댓글 쓰기 :

답변 2

map_data_i3.php


<?php
$data = [ 
    ['idx' => 1, 'name' => '가나다', 'id' => 11],
    ['idx' => 2, 'name' => null,     'id' => 12],
    ['idx' => 3, 'name' => null,     'id' => 13],
    ['idx' => 4, 'name' => null,     'id' => 14],
    ['idx' => 5, 'name' => null,     'id' => 15],
    ['idx' => 6, 'name' => null,     'id' => 16],
    ['idx' => 7, 'name' => null,     'id' => 17],
];
echo json_encode($data);
?>

 

map_data_i3_caller.php


<?php
$output = []; 
// $api_url = 'http://도메인/map_data_i3.php?id=2387';
$api_url = 'http://' . $_SERVER['HTTP_HOST'] . '/map_data_i3.php?id=2387';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
if (curl_errno($ch) != 0) {
    $output['errno'] = curl_errno($ch);
    $output['error'] = curl_error($ch);
}
$output['http_code'] = $http_code;
$output['response'] = $response;
 
curl_close($ch);
 
//echo json_encode($tree_data, true);
print('<pre>');
print_r($output);
print('</pre>');
 
$tree_data = json_decode($response, true);
 
// 1. 고정형 액세스
echo $tree_data[6]['idx'];
 
// 2. 유동형 액세스
$tree_cols = array_column($tree_data, 'idx');
echo end($tree_cols);
?>

json_encode하는값이 $tree_data인데

그건 어디서 받아오나요?

$http_code로 받아와야할것 같은데요?

답변을 작성하시기 전에 로그인 해주세요.
전체 54
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT