15
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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 );
15
9
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
15
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?