4

API から 2 つの異なる方法でファイルをダウンロードしようとしましたが、成功しませんでした。https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today

*ファイルの末尾が ではないことに注意してください.csv。ファイルのダウンロード ファイルがポップアウトされるだけです。

ダウンロードされるファイルは .CSV ファイルです。

私はCURLを使用してみました:

// Date looks like this: 2016-01-31     
$today = date("Y-m-d");

    $output_filename = "test.csv";

    $host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_REFERER, "https://www.example.com");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    print_r($result); // prints the contents of the collected file before writing..


    // the following lines write the contents to a file in the same directory (provided permissions etc)
    $fp = fopen($output_filename, 'w');
    fwrite($fp, $result);
    fclose($fp);
  • このコードで、黒い test.csv ファイルを取得しました。
  • 関数を実行した後、画面に何も出力されません ( print_r($result))

そして私は関数を使ってみましたfile_put_contents:

$today = date("Y-m-d");
echo $today;
file_put_contents("", fopen("https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today", 'r'));
// I TRIED THIS ONE TOO: 
// file_put_contents("temp.csv", "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today");

※1行目にURLが入ったCSVファイルを取得します(https://example.com/export/banana/by_date/v4?api_token=666&from=2016-01-31&to=2016-01-31)。

私がこれを正しく行っているかどうかを教えてくれる人はいますか? (これはファイルへの直接リンクではないため、おそらく私のやり方が間違っているのでしょう)。そして、これを行う正しい方法は何ですか。

4

2 に答える 2

1

ターゲット URL はhttpsおそらく、特定の ssl 固有のオプションを追加する必要があるためです

curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, realpath( '/path/to/cacert.pem' ) );

curl リクエストが失敗するもう 1 つの一般的な原因は、useragent 文字列がないことです。

curl_setopt( $ch, CURLOPT_USERAGENT, 'my useragent string' );

file_get_contentsのオプションを設定することで、使用時に同様のオプションを設定できます。$context

最後のコメントに基づいて、次を追加します。

 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
于 2016-01-31T16:09:27.150 に答える
0

これは、php から cURL を使用して https リソースにアクセスする際の (私にとっては) よく知られている問題です。デフォルト構成では証明書を検証できません。このスクリプトを機能させる最も簡単な方法は、curl_config に次の 2 行を追加することです。

$today = date("Y-m-d");

$output_filename = "test.csv";

$host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
$ch = curl_init();
$curl_config = [
    CURLOPT_URL => $host,
    CURLOPT_VERBOSE => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_AUTOREFERER => false,
    CURLOPT_REFERER => "https://www.example.com",
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_HEADER => 0,
    CURLOPT_SSL_VERIFYHOST => 0, //do not verify that host matches one in certifica
    CURLOPT_SSL_VERIFYPEER => 0, //do not verify certificate's meta
];

curl_setopt_array($ch, $curl_config); //apply config

$result = curl_exec($ch);

if (empty($result)){
    echo  curl_error($ch); //show possible error if answer if empty and exit script
    exit;
}

curl_close($ch);

print_r($result); // prints the contents of the collected file before writing..

// the following lines write the contents to a file in the same directory (provided permissions etc)
file_put_contents($output_filename, $result);
于 2016-01-31T16:08:35.050 に答える