Reputation: 1
actually I'm working on coding an API for my business, I have an issue on my code and I couldn't figure out the problem, here is the class:
<?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;
}
}
The problem that there is no data returned from the sendRequest function, just throw 'Request was not correct' exception, I tried to test the code but with no luck. I'm using xampp-win32-5.6.8-0-VC11 and I have curl-7.43.0-win64 installed,,,
Any help will be appreciated and thanks.
Edit: I use this code to test the function and it worked:
<?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);
and the result: "object(stdClass)#1 (2) { ["data"]=> array(2) { [0]=> object(stdClass)#2 (5) { ["todo_id"]=> string(10) "1323343689" ["title"]=> string(10) "test title" ["description"]=> string(28) "test description weee wasted" ["due_date"]=> string(10) "12/02/2011" ["is_done"]=> string(4) "true" } [1]=> object(stdClass)#3 (5) { ["todo_id"]=> string(10) "1323429521" ["title"]=> string(3) "wee" ["description"]=> string(11) "adsa wasada" ["due_date"]=> string(10) "12/21/2011" ["is_done"]=> string(4) "true" } } ["success"]=> bool(true) }"
So where is the problem then??
Upvotes: 0
Views: 755
Reputation: 21661
As stated in the comments you should check the curl error codes
$errorNo = curl_errno ($ch );
http://php.net/manual/en/function.curl-errno.php
In the case of a 404 or other error you have no way to know in your code as formatted.
Upvotes: 1
Reputation: 1170
Be carefull with the following line :
$result = @json_decode($result);
You are hiding any error message by using the @
.
I would suspect you are not receiving a valid json response and then $result would be set to false, because json_decode would not work.
So what happens if you not getting a valid json back? Your code should handle that error case.
In order to debug, try to add var_dump($result);
just after the curl_exec
call, that should help you understand what's wrong.
Upvotes: 1