每天进步一点点:PHP POST以及设置Header的一点探索
cn·@oflyhigh·
0.000 HBD每天进步一点点:PHP POST以及设置Header的一点探索
最近写个PHP脚本,POST一些数据给指定脚本,并且需要设置Header。 但是设置的Header不生效,为了搞明白到底哪里出了问题,做了一点学习,记录如下,备忘并供遇到同样问题的朋友参考。  (图源 :[pixabay](https://pixabay.com/)) # 回显Header 为了测试我设置成Header是否成功发送,我需要一个脚本来回显Header设置,经过学习,我发现PHP自带一个函数[getallheaders()](https://www.php.net/manual/en/function.getallheaders.php),使用这个函数可以轻易获取Header信息。 简单的示例脚本如下: ``` <?php foreach (getallheaders() as $name => $value) { echo "$name: $value "; } ?> ``` 让我们来测试一下,浏览器直接访问上述脚本,返回如下内容: > 可见上述代码工作良好,可以作为我们的测试辅助工具。 另外需要说明的是[getallheaders()](https://www.php.net/manual/en/function.getallheaders.php)其实是[apache_request_headers()](https://www.php.net/manual/en/function.apache-request-headers.php)的别名,两者是一样的。 # 发送Header 我使用PHP的[curl 函数](https://www.php.net/manual/en/ref.curl.php)来完成POST。 ``` <?php $ch = curl_init(); $url = "http://serviceuptime.net/header_echo.php"; $headers = "Accept-Encoding: gzip, deflate"; $data = "Hello!"; curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $resp= curl_exec($ch); print_r($resp); ?> ``` 代码如上所示,但是执行上述代码后,返回代码中并没有含有我们设置的内容:  那么哪里出错了呢?通过查看[curl_setopt()](https://www.php.net/manual/en/function.curl-setopt.php),我发现传递给`CURLOPT_HTTPHEADER`选项的,应该是一个数组: > 所以上述代码中,正确的写法应为: >`$headers = array("Accept-Encoding: gzip, deflate");` 再进行测试后,果然返回了我要的结果,证明Header设置成功了。 > 另外,既然是array,就可以设置多条Header了,这恰恰说明我们之前直接传入字符串的方法是错误的。 ***示例代码仅供参考*** # 相关链接 * [getallheaders()](https://www.php.net/manual/en/function.getallheaders.php) * [apache_request_headers()](https://www.php.net/manual/en/function.apache-request-headers.php) * [cURL Functions](https://www.php.net/manual/en/ref.curl.php) * [curl_setopt()](https://www.php.net/manual/en/function.curl-setopt.php) ---- <center><strong>Vote For Me As Witness</strong> https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`*** [](https://steemit.com/~witnesses) [Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1) <strong>Thank you!</strong></center>
👍 bambugrove, astros, money-way, cryptouno, eval, liangfengyouren, blues-wclouds, ezravandi, hdu, onecent, julian2013, archisteem, evelaurie, yalehazlitt, eltonwright, zorapolly, yettawild, bobarchibald, annasandy, sskiller, steeming-hot, sultan-aceh, kimzwarch, holmesian, pataty69, auntigormint, ms8988, ikonik, bhaski, lemminon, newhope, redes, steemwhalepower, penguinpablo, exec, realgoodcontent, midnightoil, cryptonized, yulan, accelerator, yyyy, cpt-sparrow, lalala, thecryptodrive, xiaohui, davidke20, bukiland, blackbunny, karja, namchau, maiyude, aellly, hmayak, a-secondchance, lingfei, wongshiying, helene, somebody, devilwsy, chinadaily, janiceting, laoyao, ethansteem, oflyhigh, deanliu, casberp, xiaokongcom, robertoueti, naturald, mynotsofitlife, deividluchi, duarte9sousa, tmarisco, kryptonauta, rea, jamzmie, andrewma, steemprotect, em3di, angelusc, busy.pay, map10k, astromaniac, wylo, technologynepal, hamsa.quality, htliao, moneybaby, kymio, phatima, yestermorrow, hyborian-strain, kiddady, canbethisone, smartdeveloper, camillagomes, aabb, imisstheoldkanye, laissez-faire, cryptohustler, huatanzhang, windowglass, rivalhw, cn-reader, cecilian, tbtek, chann, alijewel, sanzo, silentlucidity53, sweethoney, bboyady, chenlocus, khalilad, stakuza, fastiduos, winniex, abetterworld, mrliga, bearpaw, freedom-fighter, rebecca80, ethanlee, khayziljoy, razor80, weavingwords, cryptoknight27, lkvictor2005, raku, heyeshuang, speeding, chaerin, coindzs, cleateles, raili, wenjiewu, kingofdew, cnbuddy, tumutanzi, minloulou, cryptocurrencyhk, hongman96, blockchainbilly, jkkim, ebejammin, austinsandersco, jianghao, teachblogger, dgorbunov, fredo77200, agoha, resteeming, walkinharmony, asterix87, davaowhenyo, allenshayzar, historylover, btccurrency1, frankintaiwan, ravenousappetite, nitro.live, lancy, vfxness, fanso, jiangchen, that1consultant, sasaadrian, ooairbb, livinguktaiwan, oldman28, inmylife, xiaoshancun, slowwalker, gregoryhedrich, wherein, rollsman, my451r, sweetsssj, alex2016, ace108, idx, abit, partiko, goodcontentbot, adm,