你是不是暗恋我,那就给我发个消息呀,让我知道o(∩∩)o
分类: PHP
2014-06-10 13:58:38
今天用amfphp做登录接口时发现 调试页面无法带过去sessionid,阅读amfphp BackOffice\ServiceCaller.php 发现他是靠curl向默认网关发送请求的
于是改造他的curl如下
function makeAmfphpJsonServiceCall($serviceName, $methodName, $parameters = array()) {
$jsonEncodedParams = json_encode($parameters);
$requestString = "{\"serviceName\":\"$serviceName\", \"methodName\":\"$methodName\", \"parameters\":$jsonEncodedParams}";
//echo $requestString;
//写死一个存放cookie的文件,只供单人调试使用
$cookie_file=S_ROOT.'/tmp/cookie/mycookie.tmp';
if(!file_exists($cookie_file)){
$fp = fopen($cookie_file,'w+');
fclose($fp);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->amfphpEntryPointUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_POST, 1);
//指定保存cookie的文件
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
//指定发送给服务器的cookie文件
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestString);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$response = curl_exec($curl);
$decoded = json_decode($response);
//echo $response;
if(!$decoded){
throw new Exception("service called failed. server response: \n $response \nrequest: \n \n$requestString" );
}
return $decoded;
}