Heim  >  Artikel  >  Backend-Entwicklung  >  PHP cURL之CURLOPT_POSTFIELDS使用数组和字符串的区别

PHP cURL之CURLOPT_POSTFIELDS使用数组和字符串的区别

WBOY
WBOYOriginal
2016-08-08 09:19:221750Durchsuche

我们使用下面这两个变量分别做cURL POST测试:

<code><span><?php </span><span>// 数组</span><span>$arr_data</span> = [<span>'a'</span> => <span>'apple'</span>];
<span>// 字符串</span><span>$str_data</span> = <span>'{"a":"apple"}'</span>;</span></code>

然后下面是从服务器端打印出来的结果:
这里写图片描述
上面的图分别从$_SERVER[‘CONTENT_TYPE’]、$_POST变量、php://input来解析了CURLOPT_POSTFIELDS使用数组和字符串的区别。

最后,我们可以得出结论:
1)使用数组的时候

  • HTTP请求头的Content-Type会被设置成multipart/form-data;
  • 数组内容可以从$_POST里面获取;
  • 使用file_get_content(“php://input”)为空,对于这一点,PHP手册上也有说到:

    php://input is not available with enctype=”multipart/form-data”


2)使用字符串的时候
  • HTTP请求头的Content-Type会被设置成application/x-www-form-urlencoded;
  • 字符串内容会变成$_POST里面的一个元素的key;
  • 字符串内容可以完整的从php://input里获取到;

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PHP cURL之CURLOPT_POSTFIELDS使用数组和字符串的区别,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn