Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将json内容从API存储到mysql数据库_Php_Mysql_Json - Fatal编程技术网

Php 将json内容从API存储到mysql数据库

Php 将json内容从API存储到mysql数据库,php,mysql,json,Php,Mysql,Json,试图将一些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数据从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));
        }

    }


    ?>

您将您的
json``响应视为一个
array`而它是一个对象

foreach ($jsonObject as $item) {
    $title = $item->title;
}
注意
->
而不是
[''']

要进行进一步调试,请使用
var_dump()
检查变量是否包含实际对象,或者api请求是否失败

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

您的代码易受SQL注入攻击。请学习使用。使用CURL而不是
文件获取内容
出于安全原因,大多数生产环境不允许
文件获取内容
。您是否在全新的浏览器会话中尝试生成的URL,以查看生成的URL是否正常工作?你检查过你的PHP安装是否正确吗?@RowlandShaw的可能重复项不复杂我在foreach上有错误,我的是用SQLDID解决的,但var_dump帮助我,因为我得到一个空响应,所以我的请求失败
$apiURL = 'url';
$response = file_get_contents($apiUrl);
$jsonResponse = json_decode($response);
var_dump($jsonResponse);
/* 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();