实际上我正在为我的业务编写API代码,我的代码有问题,我无法弄清楚问题,这是类:
<?php
class ApiCaller
{
//some variables for the object
private $_app_id;
private $_app_key;
private $_api_url;
//construct an ApiCaller object, taking an
//APP ID, APP KEY and API URL parameter
public function __construct($app_id, $app_key, $api_url)
{
$this->_app_id = $app_id;
$this->_app_key = $app_key;
$this->_api_url = $api_url;
}
//send the request to the API server
//also encrypts the request, then checks
//if the results are valid
public function sendRequest($request_params)
{
//encrypt the request parameters
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_app_key, json_encode($request_params), MCRYPT_MODE_ECB));
//create the params array, which will
//be the POST parameters
$params = array();
$params['enc_request'] = $enc_request;
$params['app_id'] = $this->_app_id;
//initialize and setup the curl handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request
$result = curl_exec($ch);
//json_decode the result
$result = @json_decode($result);
//check if we're able to json_decode the result correctly
if( $result == false || isset($result->success) == false ) {
throw new Exception('Request was not correct');
}
//if there was an error in the request, throw an exception
if( $result->success == false ) {
throw new Exception($result->errormsg);
}
//if everything went great, return the data
return $result->data;
}
}
sendRequest函数没有返回数据的问题,只是抛出'请求不正确'异常,我试着测试代码但没有运气。我正在使用xampp-win32-5.6.8-0-VC11,我安装了curl-7.43.0-win64 ,,,
任何帮助将不胜感激,谢谢。
编辑: 我使用此代码来测试函数并且它有效:
<?php
$_app_key = '28e336ac6c9423d946ba02d19c6a2632';
$_api_url = 'http://localhost/myAPI/simpletodo_api/';
$_app_id = 'APP001';
$params = array(
'controller' => 'todo',
'action' => 'read',
'username' => 'nikko',
'userpass' =>'test1234'
);
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $_app_key, json_encode($params), MCRYPT_MODE_ECB));
$pa = array();
$pa['enc_request'] = $enc_request;
$pa['app_id'] = $_app_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pa);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$errorNo = curl_errno ($ch );
$result = @json_decode($result);
var_dump($result);
结果: “object(stdClass)#1(2){[”data“] =&gt; array(2){[0] =&gt; object(stdClass)#2(5){[”todo_id“] =&gt; string( 10)“1323343689”[“title”] =&gt; string(10)“test title”[“description”] =&gt; string(28)“test description weee wasted”[“due_date”] =&gt; string(10 )“12/02/2011”[“is_done”] =&gt; string(4)“true”} [1] =&gt; object(stdClass)#3(5){[“todo_id”] =&gt; string( 10)“1323429521”[“title”] =&gt; string(3)“wee”[“description”] =&gt; string(11)“adsa wasada”[“due_date”] =&gt; string(10)“12 / 21/2011“[”is_done“] =&gt; string(4)”true“}} [”success“] =&gt; bool(true)}”
那么问题出在哪里呢?
答案 0 :(得分:1)
请注意以下几行:
$result = @json_decode($result);
您使用@
隐藏了任何错误消息。
我怀疑你没有收到有效的json响应然后$ result会被设置为false,因为json_decode不起作用。
那么如果你没有获得有效的json,会发生什么?您的代码应该处理该错误情况。
为了进行调试,请尝试在var_dump($result);
来电后添加curl_exec
,这样可以帮助您了解错误。
答案 1 :(得分:1)
如评论中所述,您应该检查卷曲错误代码
$errorNo = curl_errno ($ch );
http://php.net/manual/en/function.curl-errno.php
如果是404或其他错误,您无法在代码中知道格式化。