2017-06-30 5 views
-1

私はPHPでカールを使用して私のGoogleシートからjsonを取得しようとしました。ただし、データは表示されません。Googleシートからjsonを得ることができませんPHPのカールを使用して

これは私のコードです:

$feed = "https://docs.google.com/spreadsheets/d/1h2fIi74s-1fypmZWkOGSwJh1ih9pfcOwQ81KTTiAfIU/edit#gid=2053646480"; 

$curl = curl_init($feed); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 

$curl_response = curl_exec($curl); 

curl_close($curl); 

$curl_json = json_decode($curl_response, true); 
print_r($curl_json); 

私は今、それはurl_fopenかかわらず無効にされている(いくつかのフォーラムFRMのコードを得た)のfopenでそれを行っていた前に、php.iniファイルで有効になっており、などの代替を探しています私は以前と同じ結果が欲しい。

<?php 
//header('Content-type: application/json'); 

// Set your CSV feed 
$feed = 'https://docs.google.com/spreadsheets/d/1sIq64_3lg9mNxidpClqdjZCZgOPbh8etCRml2cYHXeg/export?format=csv&id=1sIq64_3lg9mNxidpClqdjZCZgOPbh8etCRml2cYHXeg&gid=1790778854'; 

// Arrays we'll use later 
$keys = array(); 
$newArray = array(); 

// Function to convert CSV into associative array 
function csvToArray($file, $delimiter) { 
if (($handle = fopen($file, 'r')) !== FALSE) { 
$i = 0; 
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { 
    for ($j = 0; $j < count($lineArray); $j++) { 
    $arr[$i][$j] = $lineArray[$j]; 
    } 
    $i++; 
} 
fclose($handle); 
} 
return $arr; 
} 

// Do it 
$data = csvToArray($feed, ','); 

// Set number of elements (minus 1 because we shift off the first row) 
$count = count($data) - 1; 

//Use first row for names 
$labels = array_shift($data); 

foreach ($labels as $label) { 
$keys[] = $label; 
} 

// Add Ids, just in case we want them later 
/*$keys[] = 'id'; 

for ($i = 0; $i < $count; $i++) { 
$data[$i][] = $i; 
} 
*/ 
// Bring it all together 
for ($j = 0; $j < $count; $j++) { 
$d = array_combine($keys, $data[$j]); 
$newArray[$j] = $d; 
} 

// Print it out as JSON 
echo json_encode($newArray); 

?> 
+0

最初にこの$ curl_responseを印刷して、応答がjsonにあるかどうかを確認してください。 –

+0

このurlはjsonを返しませんが、埋め込まれたjを持つhtmlを返します。 – YvesLeBorg

+0

解決策は何ですか? –

答えて

0

は、GoogleスプレッドシートからJSONデータを取得するには、このURLを使用することができます。

https://spreadsheets.google.com/feeds/list/1h2fIi74s-1fypmZWkOGSwJh1ih9pfcOwQ81KTTiAfIU/od6/public/full?alt=json

新しいのGoogle Data APIを使用する必要があり、このAPIは廃止されることに注意してください。ここに詳細があります: https://developers.google.com/gdata/samples/spreadsheet_sample

+0

申し訳ありませんが、これは動作しません、私はカールを使用してこれをPHPで実装したい。どんな助け? –

+0

これに従うと、正しいワークシートを取得できます。 https://stackoverflow.com/questions/24531351/retrieve-google-spreadsheet-worksheet-json –

+0

私は希望の結果を得るために以下のコードを使用していました。 –

関連する問題