2017-09-19 60 views
1

我想弄清楚如何使用curl將所有視頻信息行從調用插入Youtube API。我不知道我在做什麼錯,但無論我嘗試什麼,我只能插入一行。 下面是我用隨機播放列表嘗試過的一種簡單示例。從Youtube api插入數據到Mysql

$con=mysqli_connect("localhost","...","...","..."); 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id%2C+status&playlistId=PLU12uITxBEPHfZZRTIk96NduwU_8hT-Yo&maxResults=10&key={API KEY}"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0' 
)); 

curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); 
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); 

$json_response = curl_exec($ch); 
curl_close ($ch); 

$result = json_decode($json_response, true); 

foreach ($result['items'] as $page_info) { 

$video_id = $page_info['snippet']['resourceId']['videoId']; 
$title = $page_info['snippet']['title']; 

mysqli_query($con, "INSERT INTO links (video_id, title) VALUES ('$video_id', '$title')"); 

} 

我也嘗試創建一切的數組,並試圖將其轉儲到SQL。我想我仍然會得到相同的1行結果,但我仍然嘗試。

$values = array(); 
foreach ($result['items'] as $page_info) { 

$video_id = $page_info['snippet']['resourceId']['videoId']; 
$title = $page_info['snippet']['title']; 

$values[] = "('$video_id', '$title')"; 
} 
$query_values = implode(',', $values); 
mysqli_query($con, "INSERT INTO links (video_id, title) VALUES $query_values"); 

任何人都可以幫我弄清楚我做錯了什麼?謝謝。

UPDATE:
原來我有我的表有問題,我認爲它可能已損壞或東西。我創建了一個新表並解決了我的問題。

對於任何人可能會看到這一點,並希望/試圖學習如何使用Youtube API並將數據插入到您自己的數據庫中,第一個框中的上述代碼現在可以正常工作。

只是改變:

$con=mysqli_connect("localhost","...","...","..."); 

爲了你自己的數據庫設置。例如:

$con=mysqli_connect("localhost","Username","Password","Database"); 

然後添加要插入的值,例如$ video_id,$ title並更新查詢以使用新值。
例子:

$video_id = $page_info['snippet']['resourceId']['videoId']; 
$title = $page_info['snippet']['title']; 
$description = $page_info['snippet']['description']; 
mysqli_query($con, "INSERT INTO links (video_id, title, description) VALUES ('$video_id', '$title', '$description')"); 

您還需要您的API密鑰添加到使用鍵=您的網址。
例子:

$url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id%2C+status&playlistId=PLU12uITxBEPHfZZRTIk96NduwU_8hT-Yo&maxResults=10&key=123456789123456789"; 

你可以通過這裏註冊https://console.developers.google.com/免費的API密鑰,然後單擊庫,然後選擇YouTube數據API。

要學習如何構建網址,以便您可以查詢您想要的數據,那麼這應該是一個很好的開始。 https://developers.google.com/youtube/v3/sample_requests

+0

您可以發佈var_dump($ result ['items'])的輸出嗎? – cvipul

+0

這裏是print_r。我也在下面發佈。 https://pastebin.com/88qZZ8t2 – rich

回答

0

您還需要在foreach中循環查詢mysql。如果你的implode轉換隻是根據你傳遞給implode的第一個參數從數組轉換爲字符串。相反,你這樣做。

$values = array(); 
    foreach ($result['items'] as $page_info) { 

    $video_id = $page_info['snippet']['resourceId']['videoId']; 
    $title = $page_info['snippet']['title']; 
    mysqli_query($con, "INSERT INTO links (video_id, title) VALUES ('$video_id', '$title'); 
} 
+0

在第一個例子中,我在循環中包含sql查詢,但它只插入第一行。我可以在vardump中看到所有的結果,但我只能得到插入的第一行,這就是爲什麼我很困惑。 – rich

+0

這是不可能的。請再次檢查您的mysqli_query。由於foreach迭代你的$ result ['items']。如果它只插入一個,那麼可能只有一條記錄。 – RamaKrishna

+0

這裏是print_r($ result ['items']);與我的第一個示例代碼,如果它有幫助。我只得到第一行插入,我沒有得到任何錯誤。 https://pastebin.com/88qZZ8t2 – rich