5

API からスクリーンショットを取得しようとしましたが、画像をデコードして保存すると、壊れた画像が表示されます。以下は私が使用しているコードです。テストしたい場合は、Google 応答を含むサンプル ファイルへの tinyurl を作成しました。

$name = 'test';
$result = file_get_contents('http://tinyurl.com/q4smyod'); 
$result = json_decode($result, true);
$decoded=base64_decode($result['screenshot']['data']);
file_put_contents('img/'.$name.'.jpg',$decoded);
4

3 に答える 3

14

私のコメントで述べたように、この問題は、php API を使用する際の Google 暗号化のエラーが原因で発生しています。この問題が発生している場合は、次の置換関数を使用してエンコードを修正してください。

$data    = str_replace('_','/',$result['screenshot']['data']);
$data    = str_replace('-','+',$data);
$decoded = base64_decode($data);
于 2015-04-02T07:20:02.350 に答える
0

これにより、目標に近づくことができます。名前が定義されておらず、json_decoding が少し奇妙でした:

<?php
     error_reporting(-1);
     ini_set('display_errors', 'On');
     $result = file_get_contents('http://tinyurl.com/q4smyod'); 
     $name = 'test2';
     $result = json_decode($result, true);

     $decoded = base64_encode($result['screenshot']['data']);
     file_put_contents($name.'.jpg',$decoded);

?>
于 2015-04-02T05:55:46.220 に答える