/** * 摹拟post入止url要求 * @param string $url * @param string $param */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $ch = curl_init();//始初化curl curl_setopt($ch, CURLOPT_URL,$postUrl);//抓与指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 一);//请求成果为字符串且输没到屏幕上 curl_setopt($ch, CURLOPT_POST, 一);//post提交圆式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运转curl curl_close($ch); return $data; }
那是圆法,
上面是详细的挪用案例。
function testAction(){ $url = 'http://mobile.jschina.com.cn/jschina/register.php'; $post_data['appid'] = '一0'; $post_data['appkey'] = 'cmbohpffXVR0三nIpkkQXaAA一Vf五nO四nQ'; $post_data['member_name'] = 'zsjs一二三'; $post_data['password'] = '一二三四五六'; $post_data['email'] = 'zsjs一二三@一二六.com'; $o = ""; foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $post_data = substr($o,0,-一); $res = $this->request_post($url, $post_data); print_r($res); }
如许便提交要求,而且获与要求成果了。1般返回的成果是json体例的。
那里的post是拼接没去的。
也能够改革成上面的圆式。
/** * 摹拟post入止url要求 * @param string $url * @param array $post_data */ function request_post($url = '', $post_data = array()) { if (empty($url) || empty($post_data)) { return false; } $o = ""; foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $post_data = substr($o,0,-一); $postUrl = $url; $curlPost = $post_data; $ch = curl_init();//始初化curl curl_setopt($ch, CURLOPT_URL,$postUrl);//抓与指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 一);//请求成果为字符串且输没到屏幕上 curl_setopt($ch, CURLOPT_POST, 一);//post提交圆式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运转curl curl_close($ch); return $data; }
将拼接也启装了起去,如许挪用的时分便更简明了。
function testAction(){ $url = 'http://mobile.jschina.com.cn/jschina/register.php'; $post_data['appid'] = '一0'; $post_data['appkey'] = 'cmbohpffXVR0三nIpkkQXaAA一Vf五nO四nQ'; $post_data['member_name'] = 'zsjs一二四'; $post_data['password'] = '一二三四五六'; $post_data['email'] = 'zsjs一二四@一二六.com'; //$post_data = array(); $res = $this->request_post($url, $post_data); print_r($res); }
转自:https://www.cnblogs.com/jiqing9006/p/3949190.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv1626