<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## php中curl模擬post提交多維數組 今天需要用curl模擬post提交參數,請求同事提供的一個接口;但是傳遞的參數中,有一個參數的值為數組,用普通的curl post代碼提交,會報錯誤 > PHP Notice: Array to string conversion in /test/functions.php on line 30 > Notice: Array to string conversion in /test/functions.php on line 30 代碼如下: ~~~ <?php $param = array( 'uid' => 123, 'uids' => array(12,455), 'msgType' => 'WITH', 'nick' => 'aaa', ); $url = "http://cx.com/t.php"; //通過curl的post方式發送接口請求 SendDataByCurl($url,$param); //通過curl模擬post的請求; function SendDataByCurl($url,$data=array()){ //對空格進行轉義 $url = str_replace(' ','+',$url); $ch = curl_init(); //設置選項,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定義超時3秒鐘 // POST數據 curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //執行并獲取url地址的內容 $output = curl_exec($ch); //釋放curl句柄 curl_close($ch); return $output; } ~~~ 經過修改上面代碼,可以完成提交數組的功能,而不會報php notice,代碼如下: ~~~ //通過curl模擬post的請求; function SendDataByCurl($url,$data=array()){ //對空格進行轉義 $url = str_replace(' ','+',$url); $ch = curl_init(); //設置選項,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定義超時3秒鐘 // POST數據 curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //所需傳的數組用http_bulid_query()函數處理一下,就ok了 //執行并獲取url地址的內容 $output = curl_exec($ch); $errorCode = curl_errno($ch); //釋放curl句柄 curl_close($ch); if(0 !== $errorCode) { return false; } return $output; } ~~~ ### http_build_query (PHP 5) > http_build_query -- 生成 url-encoded 之后的請求字符串描述string http_build_query ( array formdata [, string numeric_prefix] ) 使用給出的關聯(或下標)數組生成一個 url-encoded 請求字符串。參數 formdata 可以是數組或包含屬性的對象。一個 formdata 數組可以是簡單的一維結構,也可以是由數組組成的數組(其依次可以包含其它數組)。如果在基礎數組中使用了數字下標同時給出了 numeric_prefix 參數,此參數值將會作為基礎數組中的數字下標元素的前綴。這是為了讓 PHP 或其它 CGI 程序在稍后對數據進行解碼時獲取合法的變量名。 #### 例子 1. http_build_query() 使用示例 ~~~ <?php $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); echo http_build_query($data); /* 輸出: foo=bar&baz=boom&cow=milk&php=hypertext+processor */ ?> ~~~ #### 例子 2. http_build_query() 使用數字下標的元素 ~~~ <?php $data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor'); echo http_build_query($data); /* 輸出: 0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor */ echo http_build_query($data, 'myvar_'); /* 輸出: myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor */ ?> ~~~ #### 例子 3. http_build_query() 使用復雜的數組 ~~~ <?php $data = array('user'=>array('name'=>'Bob Smith', 'age'=>47, 'sex'=>'M', 'dob'=>'5/12/1956'), 'pastimes'=>array('golf', 'opera', 'poker', 'rap'), 'children'=>array('bobby'=>array('age'=>12, 'sex'=>'M'), 'sally'=>array('age'=>8, 'sex'=>'F')), 'CEO'); echo http_build_query($data, 'flags_'); /* 輸出:(為了可讀性對其進行了折行) user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%1F12%1F1956& pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap& children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8& children[sally][sex]=F&flags_0=CEO 注意:只有基礎數組中的數字下標元素“CEO”才獲取了前綴,其它數字下標元素(如 pastimes 下的元素)則不需要為了合法的變量名而加上前綴。 */ ?> ~~~ #### 例子 4. http_build_query() 使用對象 ~~~ <?php class myClass { var $foo; var $baz; function myClass() { $this->foo = 'bar'; $this->baz = 'boom'; } } $data = new myClass(); echo http_build_query($data); /* 輸出: foo=bar&baz=boom */ ?> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看