2017-06-30 4 views
-1

Ich habe versucht, Json von meinem Google-Blatt mit Curl in PHP zu bekommen. Daten werden jedoch nicht angezeigt.nicht in der Lage, Json von Google-Blatt mit Hilfe von Curl in PHP zu bekommen

dies ist mein Code:

$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); 

, bevor ich hatte es mit fopen getan (erhaltener Code frm einige Forum) jetzt hat es deaktiviert wurde, obwohl url_fopen in der php.ini-Datei und auf der Suche nach einer Alternative aktiviert ist als Ich möchte das gleiche Ergebnis wie zuvor.

<?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

Zuerst drucken Sie diese $ curl_response und überprüfen Sie, ob die Antwort in JSON ist. –

+0

diese URL gibt json nicht zurück, aber etwas html mit eingebetteter js. – YvesLeBorg

+0

was wäre die lösung ?? –

Antwort

0

Sie können diese URL verwenden, um JSON-Daten aus einer Google-Tabelle abzurufen.

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

Bitte beachten Sie, dass diese API ist veraltet, können Sie die neue Google Data API verwenden sollten. Hier finden Sie weitere Informationen: https://developers.google.com/gdata/samples/spreadsheet_sample

+0

Sorry das funktioniert nicht, ich möchte dies in PHP mit Curl implementieren. irgendeine Hilfe? –

+0

Sie können folgen, um das richtige Arbeitsblatt zu erhalten, es funktioniert für mich. https://stackoverflow.com/questions/24531351/retrieve-google-spreadsheet-worksheet-json –

+0

Ich habe folgenden Code zuvor verwendet, um das gewünschte Ergebnis zu erhalten. –

Verwandte Themen