记一次yii下curl排错经验:
要求需要通过header传送token,数据传入方式为application/json
1.postman方式调取,没有问题,参数已json形式传过去-{}
2.原生PHP调取
$url = Yii::$app->params['get_auto_test_detail_list']; $params['start'] = 0; $params['count'] = 10; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); //不是json_encode(http_build_query($params)) $a = "Bearer eyJ0eXAiOi";
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Authorization: " .$a, "Content-Type:application/json")); //数组形式,参数名字是CURLOPT_HTTPHEADER
$output = curl_exec($ch);
if($output === FALSE ){ echo "CURL Error:".curl_error($ch); } curl_close($ch); var_dump($output);die;
3.Yii 通过Curl类 _simple_call()方法调取,
public function getAutoTestDetailList()
{ $url = Yii::$app->params['get_auto_test_detail_list']; $params['start'] = 0; $params['count'] = 10; $curl = new Curl(); $options['HTTPHEADER'] = array("Authorization:" .self::TOKEN, "Content-Type:" .self::CONTENT_TYPE); //封装方法里面对httpheader有拼接,调用set_opt_array() $result = $curl->_simple_call("post", $url, json_encode($params), $options); //参数json形式 if ($result !== false) { $result = json_decode($result, true); }
echo "<pre>";var_dump($result);echo "</pre>"; }
1.get方式传值
1 function testGet(){ 2 $ch = curl_init (); //初始化一个cURL会话 3 $url = "127.0.0.1/testPage?test=test";
4 5 curl_setopt ( $ch, CURLOPT_URL, $url ); //设置一个cURL传输选项,url链接 6 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); //是否传头信息 7
8 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //是否流,获取数据返回,不设置默认将抓取数据的结果返回,成功返回bool(true),失败返回bool(false) 9 $content = curl_exec ( $ch ); //关闭链接 10 var_dump($content); 11 12 } 13 14 15 16 function testPage(){ 17 18 echo $_GET['test']; 19 }
2.post方式传值
function testPost(){ $ch = curl_init (); $data["test"] = "test"; $url = "127.0.0.1/test"; curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POST, 1 ); //如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //post方式传数据 $content = curl_exec ( $ch ); curl_close ( $ch ); var_dump($content); } function testPage(){ echo $_POST['test']; }
3.关于php curl 编码方式 的content-type 是 multipart/form-data 还是 application/x-www-form-urlencoded
传递一个数组到CURLOPT_POSTFIELDS,CURL会把数据编码成 multipart/form-data,而传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded"。
http://www.phperz.com/article/14/1031/32420.html 这个链接说了下两种编码的区别。而如果将post方式传输的数据 惊醒http_build_query($data) 后 ,再传输就是
application/x-www-form-urlencoded 格式 。
4.一个较为ok的例子
1 class RemoteToolService { 2 3 static public $timeout = 10; 4 5 /** 6 * send request 7 * 8 * @param $url 9 * @param $type 10 * @param $args 11 * @param $charset 12 * 13 * @Returns 14 */ 15 public function send($url, $type, $args, $charset = 'utf-8') { 16 if ($type == 'post') { 17 $returnValue = $this->_post($url, $args, $charset); 18 } else { 19 $url .= '?' . http_build_query($args); 20 $returnValue = $this->_get($url, $charset); 21 } 22 return $returnValue; 23 } 24 25 private function _post($url, $arguments, $charset = 'utf-8') { 26 if (is_array($arguments)) { 27 $postData = http_build_query($arguments); 28 } else { 29 $postData = $arguments; 30 } 31 32 $ch = curl_init(); 33 curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); 34 curl_setopt($ch, CURLOPT_URL, $url); 35 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 36 curl_setopt($ch, CURLOPT_POST, 1); 37 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 38 curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout); 39 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); 40 41 $returnValue = curl_exec($ch); 42 curl_close($ch); 43 if ($charset != 'utf-8') { 44 $returnValue = iconv($charset, 'utf-8', $returnValue); 45 } 46 return $returnValue; 47 } 48 49 private function _get($url, $charset = 'utf-8') { 50 $ch = curl_init(); 51 curl_setopt($ch, CURLOPT_URL, $url); 52 curl_setopt($ch, CURLOPT_HEADER, 0); 53 curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout); 54 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); 55 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 56 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 57 $returnValue = curl_exec($ch); 58 curl_close($ch); 59 if ($charset != 'utf-8') { 60 $returnValue = iconv($charset, 'utf-8', $returnValue); 61 } 62 return $returnValue; 63 } 64 65 public function sendJson($url, $arguments, $charset = 'utf-8') { 66 if (is_array($arguments)) { 67 $postData = json_encode($arguments); 68 } else { 69 $postData = $arguments; 70 } 71 72 $ch = curl_init(); 73 curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); 74 curl_setopt($ch, CURLOPT_URL, $url); 75 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 76 curl_setopt($ch, CURLOPT_POST, 1); 77 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 78 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($postData))); 79 curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout); 80 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); 81 82 $returnValue = curl_exec($ch); 83 curl_close($ch); 84 if ($charset != 'utf-8') { 85 $returnValue = iconv($charset, 'utf-8', $returnValue); 86 } 87 return $returnValue; 88 } 89 90 } 91 92 $params = array( 93 'uid' => $uid, 94 'incr' => $incr, 95 'type' => $type, 96 'source' => $scorce, 97 'sign' => $sKey 98 ); 99 $ob = new RemoteToolService(); 100 $data =$ob->send($callUrl, 'post', $params);
对curl的了解还是不太清晰,只是暂时能用。。