在這篇文章中僅限於描述http請求調用接口的方式,我一直是在用thinkphp的框架開發,thinkphp的源碼裏面有自帶的http的擴展類庫。可是並無包含http的請求(get或者post請求的接口調用),因此這個調用接口的類庫是不要我本身來動手實現的。這個時候就須要我本身使用底層代碼來實現了。
php
若是隻是簡單的請求,file_get_contents就夠了,可是通常狀況下調用接口咱們會使用curl,開啓php的curl的擴展配置。由於curl是支持post方式調用接口的,相對來講會更加安全,參數更多,功能更強。html
file_get_contents的例子:thinkphp
<?php function Post($url, $post = null) { if (is_array($post)) { ksort($post); $content = http_build_query($post); $content_length = strlen($content); $options = array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: $content_length\r\n", 'content' => $content ) ); return file_get_contents($url, false, stream_context_create($options)); } } $data = array ( 'url' => 'http://www.waqiang.com/index.php/url/shorten', 'submit' => 'submit', ); $response = Post('http://www.waqiang.com/index.php/url/shorten', $data); $reg = '#[\'"](http:(//|\\/\\/)t\.cn((/|\\/)([^\'"/]+)(/|\\/)?|(/|\\/)))[\'"]#'; preg_match_all($reg, $response, $match); var_dump($match); ?>
curl的例子:瀏覽器
<?php function shorturl($long_url) { $url = 'http://www.waqiang.com/index.php/url/shorten'; $data = array( 'url' => $long_url, 'submit' => 'Submit' ); $curlObj = curl_init(); $options = array( CURLOPT_URL => $url, CURLOPT_POST => TRUE, //使用post提交 CURLOPT_RETURNTRANSFER => TRUE, //接收服務端範圍的html代碼而不是直接瀏覽器輸出 CURLOPT_TIMEOUT => 4, CURLOPT_POSTFIELDS => http_build_query($data), //post的數據 ); curl_setopt_array($curlObj, $options); $response = curl_exec($curlObj); curl_close($curlObj); return $response; } $result = shorturl('http://www.waqiang.com/index.php/url/shorten'); $reg = '#[\'"](http:(//|\\/\\/)t\.cn((/|\\/)([^\'"/]+)(/|\\/)?|(/|\\/)))[\'"]#'; preg_match_all($reg, $result, $match); var_dump($match); ?>
還有一種方式,就是socket鏈接host發送請求,php中的函數會使用到fsockopen,fwrite,feof,fread,fclose這些。安全
socket方式例子【PHP fsockopen須要 PHP.ini 中 allow_url_fopen 選項開啓。】:app
<?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); ?>
另外,stream_context系列的函數也是能夠實現相似的功能。框架
示例以下:curl
<?php function do_post_request($url, $postdata, $files = null) { $data = ""; $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); //Collect Postdata foreach($postdata as $key => $val) { $data .= "--$boundary\n"; $d ata .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; } $data .= "--$boundary\n"; //Collect Filedata foreach($files as $key => $file) { $fileContents = file_get_contents($file['tmp_name']); $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n"; $data .= "Content-Type: image/jpeg\n"; $data .= "Content-Transfer-Encoding: binary\n\n"; $data .= $fileContents."\n"; $data .= "--$boundary--\n"; } $params = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary='.$boundary, 'content' => $data )); $ctx = stream_context_create($params); $fp = fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response; } $postdata = array( 'name' => $_POST['name'], 'age' => $_POST['age'], 'sex' => $_POST['sex'] ); $files['image'] = $_FILES['image']; do_post_request("http://www.jb51.net", $postdata, $files); ?>