I am trying to get the top deals using the flipkart product feed api in codeigniter.
This is the piece of code that I have written to achieve it.
public function getTopDealsFlipkart()
{
$data = array();
$data['url'] = 'https://affiliate-api.flipkart.net/affiliate/1.0/feeds/'.FK_AFFILIATE_ID.'/category/mens-clothing.json?expiresAt=1510123592000&sig=<urlValidationSignature>&inStock=true';
$header['Accept'] = 'application/json';
$header['Fk-Affiliate-Token'] = FK_AFFILIATE_TOKEN;
$header['Fk-Affiliate-Id'] = FK_AFFILIATE_ID;
$data['header'] = $header;
$output = $this->get_response($data);
return $output;
}
public function get_response($data)
{
$output = null;
try {
$client = new GuzzleHttp\Client();
$response = $client->request('GET', $data['url'], [
'headers' => $data['header']
]);
$output = $response->getBody();
} catch (Exception $e) {
log_message('debug', 'fail to get response ');
}
return $output;
}
The output that it is returning is null.
I am not able to understand what is 'sig' as the API just defines it as "url validation signature". There is no information further information on the API. What is the URL validation signature and How do I generate the URL validation signature?
Related
I am brand new to PSR standards, and I am not sure if I adapted my code to PSR-7, PSR-15 correctly.
My code is handling a POST request to delete a group of products by receiving an array of ids.
Is that a correct adaptation? Thanks.
<?php
require_once 'DataBase.php';
require_once 'config.php';
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class DeleteRequest implements RequestHandlerInterface
{
private $DB;
public function __construct(DataBase $DB)
{
$this->DB = $DB;
}
//Delete each product from the database using the ID
public function handle(ServerRequestInterface $request): ResponseInterface
{
// Make sure it is a POST request
if ($request->getMethod() !== 'POST') {
throw new Exception('Incorrect REQUEST_METHOD. '.
'Only POST requests are allowed.');
}
// Extract the 'ids' array from the request data
MyLogV($request->getBody()->getContents());
$data = json_decode($request->getBody()->getContents(), true);
// Make sure the 'ids' array is present in the data
if (!isset($data['ids'])) {
throw new Exception('Missing required parameter: ids');
}
$ids = $data['ids'];
foreach ($ids as $id) {
myLog("DeleteRequest->handle","id",$id);
$result = $this->DB->deleteProduct($id);
if ($result['status'] != 'success') break;
}
// Generate the response: 200 => OK, 400 => Bad request
$status = $result['status'] == 'success' ? 200 : 400;
$response = new JsonResponse($result, $status);
myLogV($result['status']);
return $response;
}
}
try {
$serverRequest = ServerRequestFactory::fromGlobals();
$DB = new DataBase();
$deleteRequest = new DeleteRequest($DB);
$response = $deleteRequest->handle($serverRequest);
$response->send();
} catch (Exception $e) {
myLog("delete.php","Exception",$e->getMessage());
$result = ['status' => 'error','message'=> $e->getMessage()];
$response = new JsonResponse($result, 400);
$response->send();
}
exit();
?>
I tried to understand the PSR standards.
I am using cakephp 2.10.24. I need to use the variable returned from the controller in a php file(not a view), so I used the dispatch function to make a Cakephp request. The data is displayed by itself and I'm not able to execute some code after the dispatch call.
I tried adding to the controller but it didn't work
$this->autoRender = false;
$this->layout = false;
$this->autoLayout = false;
the controller action:
$this->autoRender = false;
$this->layout = false;
$this->autoLayout = false;
$this->response->body(json_encode(array(
'key' => 0,
'message' => 'Invalid request.'
)));
$this->response->send();
$this->_stop();
php file:
<?php
echo ('i\'m test file <br />');
include 'app/webroot/index.php';
$request = new CakeRequest('/controller/action/param');
$response = new CakeResponse(array('type' => 'application/json'));
echo $results = $Dispatcher->dispatch(
$request ,
$response,
array('return' => 'vars')
);
//some codes not running
var_dump($response);
print_r(json_decode($response));
?>
Got the results but still auto printing the result on the screen.
test file:
$dispatcher = new Dispatcher();
$response = new CakeResponse();
$results = $dispatcher->dispatch(new CakeRequest('/Controlller/Action/params'),$response);
print_r("response: ".$response);
action in the controller:
$this->autoRender=false;
$this->autoLayout=false;
$this->response->body($result);
$this->response->send();
While trying to request data from en external API, I want to control how the response is being passed to my view or database. However what would be the correct way to write the code below, so instead of simply echoing the data onto the view I would like to store it inside an object that I can pass to my view or model in a more controlled way?
public function index()
{
$contents = $this->saveApiData();
return View::make('stats.index')->with('contents', $contents);
}
public function saveApiData()
{
$client = new Client(['base_uri' => 'https://owapi.net/api/v3/u/']);
$res = $client->request('GET', "data" . "/blob");
echo $res->getStatusCode();
echo $res->getBody();
}
Just put them together in an array and return it. You never echo data in a function to return them.
public function saveApiData()
{
$client = new Client(['base_uri' => 'https://owapi.net/api/v3/u/']);
$res = $client->request('GET', "data" . "/blob");
$contents = [
'status' => $res->getStatusCode(),
'body' => $res->getBody()
];
return $contents;
}
im working with magento api, and i need verify my connect.
how to call method APIauthentication with $client object? because im getting error on this: Error: Function ("APIauthentication") is not a valid method for this service
thanks for the help.
this is my class:
<?php
class Magento {
const PRODUCTS_LIST = 'catalog_product.list';
public function Verify( $data )
{
$client = new SoapClient( $data['store_url'] );
$verify = $client->APIauthentication( $data['api_user'], $data['api_key'] );
if ($verify)
{
return $this->Register( $data['store_url'], $data['api_user'], $data['api_key'] );
}
}
public function APIauthentication( $apiUser, $apiKey ) {
$client = $this->_getClient();
$token = $client->login( $apiUser, $apiKey );
$this->_setToken( $token );
return $this->_apiJsonResult( $token );
}
}
there is url:
$data['store_url'] = 'http://localhost:8888/magento/api/soap/?wsdl';
firstly i need verify, second - get list:
// For products
public function getProducts()
{
return $client->APIgetProductsList();
}
/*
* Get product list
*/
public function APIgetProductsList() {
$token = $this->_getToken();
$client = $this->_getClient();
$products = $client->call($token, self::PRODUCTS_LIST );
return $this->_apiJsonResult( $products );
}
You need to create your own api by creating new module then you can use that api method refer this link http://www.magentocommerce.com/wiki/5_-_modules_and_development/web_services/custom_api_complete_example
I am trying to get a soap response in php. It keeps coming as an object onto my web browser but not as xml. WSDL shows as XML but not the response received. Below is my server side code. The soap server is Zend Soap
ini_set("soap.wsdl_cache_enabled", 0);
if (isset($_GET['wsdl'])){
$wsdl = 'http://localhost/webservice/soap';
$autoDiscover = new AutoDiscover();
$autoDiscover->setOperationBodyStyle(
array('use' => 'literal',
'namespace' => 'http://localhost/webservice/soap')
);
$autoDiscover->setBindingStyle(
array('style' => 'rpc',
'transport' => 'http://schemas.xmlsoap.org/soap/http')
);
$autoDiscover->setComplexTypeStrategy(new ArrayOfTypeComplex());
// $service is the class that does the handling of functions
$autoDiscover->setClass($service);
$autoDiscover->setUri($wsdl);
$response->getHeaders()->addHeaderLine('Content-Type', 'text/xml');
$response->setContent($autoDiscover->toXml());
} else {
$server = new Server('http://localhost/webservice/soap?wsdl'
);
// $service is the class that does the handling of functions
$server->setObject($service);
$response->setContent($server->handle());
}
return $response;
}
Service class
class service
{
/**
*
* #param string $Email
* #return int $Credit
*/
public function checkCredits($Email)
{
$validator = new email();
if (!$validator->isValid($Email))
{
return new \SoapFault('5', 'Please Provide an Email');
}
$rowset = $this->tableGateway->select(array('EMAIL'=>$Email))
$row = $rowset->current();
$credits = $row->CREDITS;
return $credits;
}
}
Request is :
try{
$sClient = new SoapClient('http://localhost/webservice/soap?wsdl');
$params = "email";
$response = $sClient->checkCredits($params);
var_dump($response);
} catch(SoapFault $e){
var_dump($e);
}
This is an example of how I handle my functions with SoapClient:
$client = new SoapClient('http://url/Service.svc?wsdl');
$var = array('arg' => 10,
'VA' => 48);
$varresponse = $client->Function($var);
print_r( $varresponse->FunctionResult);
Hope this will help you out.
Your soapserver should look a bit like this:
<?php
if(!extension_loaded("soap")){
dl("php_soap.dll");
}
ini_set("soap.wsdl_cache_enabled","0");
$server = new SoapServer("hello.wsdl");
function doHello($yourName){
return "Hello, ".$yourName;
}
$server->AddFunction("doHello");
$server->handle();
?>
How did you set up yours? Do you return anything?
Now, your client should look like this:
<?php
try{
$sClient = new SoapClient('http://localhost/test/wsdl/hello.xml');
$params = "Name";
$response = $sClient->doHello($params);
var_dump($response);
} catch(SoapFault $e){
var_dump($e);
}
?>