file_get_contents和curl对于post方式的解决办法
post方式解决办法
其实很简单,我们只要仔细看看就知道了。。。
file_get_contents:
$content=$_POST['content'];
$access_token=$_POST['access_token']; //post接收
$data = array("access_token" => $access_token,"status" => $content);
$data = http_build_query($data);//转化
$opts = array(
'http'=>array(
'method'=>"POST", //post方式传值
'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
"Content-length:".strlen($data)."\r\n" .
"Cookie: foo=bar\r\n" .
"\r\n",
'content' => $data, //传值内容
)
);
$cxContext = stream_context_create($opts);
$sFile = file_get_contents("https://api.weibo.com/2/statuses/update.json", false, $cxContext);
echo $sFile;
curl :
我们首先可以引用一个类文件,在根目录下(./curl.php),具体看下面吧!!
<?php
include './curl.php';
$code=$_GET['code'];
$arr = array(
'client_id'=>'xxx', //(xxx代表我们自己获取到的授权内容)
'client_secret'=>'xxx',
'grant_type'=>'xxx',
'code'=>$code,
'redirect_uri'=>'xxx'
);
$url="https://api.weibo.com/oauth2/access_token?client_id=xxx&client_secret=xxx&grant_type=authorization_code&code=$code&redirect_uri=xxx";
$a=curl($url,$arr,'POST');
//var_dump($a);
$res=json_decode($a,true);
$token=$res['access_token'];
print_r($token);die;//这就成功了
希望能够帮助大家!!!