2017-06-20 135 views
-1

试图从API存储一些json数据到MySQL。将json内容从API存储到mysql数据库

我用这简单的PHP代码

<?php 

    require 'config.php'; 

    $url = "https://www.widgety.co.uk/api/operators.json?app_id<key>&token<token>"; 
    $string = file_get_contents('https://www.widgety.co.uk/api/operators.json?app_id<key>&token=<token>'); 
    $arr = json_decode($string, true); 

    foreach($arr as $item){ 
     $title   = $item['operator']['title']; 
     $id    = $item['operator']['id']; 
     $profile_image = $item['operator']['profile_image_href']; 
     $cover_image = $item['operator']['cover_image_href']; 
     $href   = $item['operator']['href']; 
     $html_href  = $item['operator']['html_href']; 
     $unique_text = $item['operator']['unique_text']; 
     $reasons_to_book = $item['operator']['reasons_to book']; 
     $members  = $item['operator']['members_club']; 
     $brochures  = $item['operator']['brochures']; 
     $ships   = $item['operator']['ships']; 
     $video_url  = $item['operator']['video_url']; 

     $query("INSERT INTO operator (title, id, profile_image) VALUES('$title', '$id', '$profile_image')"); 

     $dataatodb = mysqli_query($con, $query); 

     if(!$dataatodb){ 
      die("Error" . mysqli_error($con)); 
     } 

    } 


    ?> 

但我得到这个错误

Warning: file_get_contents(app_id<key>&token<token>): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp\htdocs\mypage\dataload.php on line 6 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\mypage\dataload.php on line 9 

我在做什么错?

PS网址]我有权利APP_ID和令牌

+0

您的代码容易受到SQL注入。请学习使用[预先准备的语句](https://www.youtube.com/watch?v=nLinqtCfhKY)。 –

+1

使用CURL而不是'file_get_contents'出于安全原因,大多数生产环境不允许使用'file_get_contents'。 –

+0

您是否在全新的浏览器会话中尝试了生成的URL以查看生成的URL是否正常工作?你是否检查过你的PHP安装[配置为允许打开url](http://us2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen)? –

回答

1

你对待你的json``response as an array`而它是一个对象。检查->,而不是['']

为了进一步调试使用var_dump()

foreach ($jsonObject as $item) { 
    $title = $item->title; 
} 

采取通知,如果你的变量包含实际的对象,如果您的API请求失败。

$apiURL = 'url'; 
$response = file_get_contents($apiUrl); 
$jsonResponse = json_decode($response); 
var_dump($jsonResponse); 
+0

没有解决它,但var_dump帮我导致我得到一个NULL响应,所以我的请求失败 – Maria

1
/* create a connection */ 
$mysqli = new mysqli("localhost", "root", null, "yourDatabase"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

/* let's say we're grabbing this from an HTTP GET or HTTP POST variable called jsonGiven... */ 
$jsonString = $_REQUEST['jsonGiven']; 
/* but for the sake of an example let's just set the string here */ 
$jsonString = '{"name":"jack","school":"colorado state","city":"NJ","id":null} 
'; 

/* use json_decode to create an array from json */ 
$jsonArray = json_decode($jsonString, true); 

/* create a prepared statement */ 
if ($stmt = $mysqli->prepare('INSERT INTO testdemo (name, school, city, id) VALUES (?,?,?,?)')) { 

    /* bind parameters for markers */ 
    $stmt->bind_param("ssss", $jsonArray['name'], $jsonArray['school'], $jsonArray['city'], $jsonArray['id']); 

    /* execute query */ 
    $stmt->execute(); 

    /* close statement */ 
    $stmt->close(); 
} 

/* close connection */ 
$mysqli->close(); 
相关问题