记一次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'] = ;
$params['count'] = ;
$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方式传值
function testGet(){
$ch = curl_init (); //初始化一个cURL会话
$url = "127.0.0.1/testPage?test=test";
4
curl_setopt ( $ch, CURLOPT_URL, $url ); //设置一个cURL传输选项,url链接
curl_setopt ( $ch, CURLOPT_HEADER, 0 ); //是否传头信息
7
8 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //是否流,获取数据返回,不设置默认将抓取数据的结果返回,成功返回bool(true),失败返回bool(false)
$content = curl_exec ( $ch ); //关闭链接
var_dump($content); } function testPage(){ echo $_GET['test'];
}
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的例子
class RemoteToolService { static public $timeout = 10; /**
* send request
*
* @param $url
* @param $type
* @param $args
* @param $charset
*
* @Returns
*/
public function send($url, $type, $args, $charset = 'utf-8') {
if ($type == 'post') {
$returnValue = $this->_post($url, $args, $charset);
} else {
$url .= '?' . http_build_query($args);
$returnValue = $this->_get($url, $charset);
}
return $returnValue;
} private function _post($url, $arguments, $charset = 'utf-8') {
if (is_array($arguments)) {
$postData = http_build_query($arguments);
} else {
$postData = $arguments;
} $ch = curl_init();
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); $returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} private function _get($url, $charset = 'utf-8') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} public function sendJson($url, $arguments, $charset = 'utf-8') {
if (is_array($arguments)) {
$postData = json_encode($arguments);
} else {
$postData = $arguments;
} $ch = curl_init();
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($postData)));
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); $returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} } $params = array(
'uid' => $uid,
'incr' => $incr,
'type' => $type,
'source' => $scorce,
'sign' => $sKey
);
$ob = new RemoteToolService();
$data =$ob->send($callUrl, 'post', $params);
对curl的了解还是不太清晰,只是暂时能用。。