3
1

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.

cURLでPOSTする際にCURLOPT_POSTFIELDSで渡す値が文字列か配列かでheaderのContent-Typeが変わるの知らなくて詰んだ

Last updated at Posted at 2018-09-05
$data = [
	'a' => 1,
	'b' => 2,
	'c' => 3,
];

//中略

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

//中略

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
print curl_getinfo($ch, CURLINFO_HEADER_OUT);	//Content-Type: application/x-www-form-urlencoded; boundary=***

//中略

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&') );
print curl_getinfo($ch, CURLINFO_HEADER_OUT);	//Content-Type: application/x-www-form-urlencoded

はいくそー。

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded') );

って設定しておいても一緒。
仕様考えればそうなんだろうけど、REST APIによっては任意のContent-Typeしか受け入れなくなっていたりするので注意しましょうって話。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?