LoginSignup
14
8

More than 5 years have passed since last update.

phpのcurlの結果はデフォルトで"出力"されるので注意

Last updated at Posted at 2015-11-19

php上級者には当たり前なんだろうけどハマったので。

気付いた経緯

  1. ファイルをダウンロードさせるphpの処理の途中で別サーバのAPIをcurlで叩く処理を追加
  2. テストしている時にダウンロードしたデータの先頭にAPIのレスポンスボディが混ざっているのに気付く
  3. 😇

対策

CURLOPT_RETURNTRANSFER をtrueにする。なぜデフォルトで出力されるのか理解に苦しむ。。

$ch = curl_init ( "http://host/path" );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $param);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_exec ( $ch );
curl_close ( $ch );
14
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
8