php请求url的方式
//curl 请求 $url
//
- 初始化连接句柄;
- 设置CURL选项;
- 执行并获取结果;
- 释放VURL连接句柄
$curl = curl_init(); //初始化连接句柄 curl_setopt($curl, CURLOPT_URL, $url); //设置curl选项 curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); $data = curl_exec($curl);//执行查询
第二种请求方式
$d = [ "is_express" => "2", "order_id" => $order_data['order_id'], "dd" => "1", "id" => $order_data['mall_id'], 'express_content' => '充值成功' ]; //发货 $r = file_get_contents('http://xiaowei.shop.test/web/index.php?r=api/test/fa&' . http_build_query($d)); $res = json_decode($r, true);
curl post和get请求方法
function http_get($url) { $oCurl = curl_init(); if (stripos($url, "https://") !== FALSE) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 }
// 如果需要header设置内容
curl_setopt($oCurl, CURLOPT_URL, $url);
// curl_setopt($0Curl,CURLOPT_HTTPHEADER,$header) //设置请求头:$header =["cookie:缓存值",'Content-type: text/plain', 'Content-length: 100'] curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) { return $sContent; } else { return false; } } function http_post($url, $param, $post_file = false) { $oCurl = curl_init(); if (stripos($url, "https://") !== FALSE) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (is_string($param) || $post_file) { $strPOST = $param; } else { $aPOST = array(); foreach ($param as $key => $val) { $aPOST[] = $key . "=" . urlencode($val); } $strPOST = join("&", $aPOST); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);// curl_setopt($oCurl, CURLOPT_POST, true); curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) { return $sContent; } else { return false; } }
第三种fopen()函数;
三、fopen()函数 $file = fopen("http://www.baidu.com", "r") or die("打开远程文件失败!");
第四种
4.fsockopen方式(只能获取网站主页信息,其他页面不可以) $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br /> "; } else { $out="GET / HTTP/1.1 "; $out.="Host: www.example.com "; $out.="Connection: Close "; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }