PHP 发送post请求
/** * 发送post请求 * @param string $url 请求地址 * @param array $data post键值对数据 * @return string */ function https_post($url, $data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
/** * PHP发送Json对象数据 * @param $url 请求url * @param $data 发送的json字符串/数组 * @return array */ public static function json_post($url, $data = NULL) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); if(!$data){ return 'data is null'; } if(is_array($data)) { $data = json_encode($data); } curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data), 'Cache-Control: no-cache', 'Pragma: no-cache' )); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); $errorno = curl_errno($curl); if ($errorno) { return $errorno; } curl_close($curl); return $res; }
另一种方法
public function getPost(){ $url=$data['url']; //请求地址 $data=json_encode([ 'begin_date'=>'20210501', 'end_date'=>'20210501' ]); $opts=[ 'http'=>[ 'method'=>'POST', 'header'=>[ 'Content-Type:application/json', 'Charset:utf8' ], 'content'=>$data ] ]; $context=stream_context_create($opts); $jsondata=file_get_contents($url,false,$context); return $jsondata; }
还有一种方法 表单传值跳转
echo "<form style='display:none;' id='form1' name='form1' method='post' action='https://www.zhix.net/'> <input name='goodsname' type='text' value='".$goodsname."' /> <input name='istype' type='text' value='".$istype."'/> <input name='key' type='text' value='".$key."'/> <input name='notify_url' type='text' value='".$notify_url."'/> <input name='orderid' type='text' value='".$orderid."'/> <input name='orderuid' type='text' value='".$orderuid."'/> <input name='return_url' type='text' value='".$return_url."'/> <input name='price' type='text' value='".$price."'/> <input name='uid' type='text' value='".$uid."'/> </form> <script type='text/javascript'>function load_submit(){document.form1.submit()}load_submit();</script>";