最近在做微博授权的功能,我拿到了第一次的code,但是第二次去换access_token出错显示{"error":"HTTP METHOD is not suported for this request!","error_code":10021,"request":"/oauth2/access_token"},查资料知道是提交必须是post请求。
https://api.weibo.com/oauth2/access_token?client_id=101393&client_secret=a2d375c81a&grant_type=authorization_code&redirect_uri=http://127.0.0.1:8080/scrm/api/weibo/bindGetAccessToken&code=1101a1c76b3;这是我第二次的地址,我怎么能在后台通过post方式发送请求呢,急求,问了很多同事,也没想出办法。
转发的话这个地址会加上原有地址,重定向又需要传参数还得是post,查资料试过httpclient,redirectview都没用!!
使用curl 用post请求获取数据
$this->http_info = array();
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
curl_setopt($ci, CURLOPT_HEADER, FALSE);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
case 'DELETE':
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (!empty($postfields)) {
$url = "{$url}?{$postfields}";
}
}
if ( isset($this->access_token) && $this->access_token )
$headers[] = "Authorization: OAuth2 ".$this->access_token;
$headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];
curl_setopt($ci, CURLOPT_URL, $url );
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
$response = curl_exec($ci);
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$this->http_info = array_merge($this->http_info, curl_getinfo($ci));
$this->url = $url;
if ($this->debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info====='."\r\n";
print_r( curl_getinfo($ci) );
echo '=====$response====='."\r\n";
print_r( $response );
}
curl_close ($ci);
return $response;
第一,感谢帮助。后来问题解决了。因为当时第一次做微博授权。并不知道微博给的api里面有相关httpclient的封装。后来找到那个类,调用就好了。