2017-06-20 10 views
-1

APIからjsonデータをMySQLに保存しようとしています。APIからmysqlデータベースにjsonコンテンツを保存する

私はこの単純な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 URLに私はそれがオブジェクトである間、あなたのjson``response as an array`を扱っている右APP_IDとトークン

+0

あなたのコードは、SQLインジェクションに対して脆弱です。 [prepared statements](https://www.youtube.com/watch?v=nLinqtCfhKY)を使用する方法を学んでください。 –

+1

'file_get_contents'の代わりにCURLを使用すると、ほとんどの本番環境ではセキュリティ上の理由から' file_get_contents'が許可されません。 –

+0

生成されたURLが新しいブラウザセッションで試してみましたか?あなたのPHPインストールが[URLを開くことを許可するように設定されている](http://us2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen)であることを確認しましたか? –

答えて

1

を持っています。

foreach ($jsonObject as $item) { 
    $title = $item->title; 
} 
->の代わりに、あなたの変数が実際のオブジェクトが含まれているか、あなたのAPIリクエストが失敗した場合かどうかを確認するためにさらにデバッグ用 var_dump()については ['']

テイク通知。

$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(); 
関連する問題