0

I have this code where i try to list all my articles in my webshop via API (documentation here http://api.textalk.se/webshop/)

I made similar script witch receives orders from the same API and it works perfect, but this i wont get to work.

Any ideas what i am doing wrong?

 <?php
class TextalkApi
{
       public function AdminLogin($username, $password) {
        $url = 'https://shop.textalk.se/backend/jsonrpc/v1/';           

        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
        curl_setopt($ch, CURLOPT_POST, 1);      
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json-rpc'));   
        curl_setopt($ch, CURLOPT_POSTFIELDS, '{"jsonrpc":"2.0", "id":1, "method":"Admin.login", "params":["' . $username . '","' . $password . '"]}');  
        $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($http_code != 200) 
            throw new Exception('Error : Admin Login Failed');

        return $data['result'];
    }

           public function GetArts($admin_auth_token, $webshop_id) {
        $url = 'https://shop.textalk.se/backend/jsonrpc/v1/?auth=' . $admin_auth_token . '&webshop=' . $webshop_id;

        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
        curl_setopt($ch, CURLOPT_POST, 1);      
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json-rpc'));

// this below i beleive dosen't work
        curl_setopt($ch, CURLOPT_POSTFIELDS, 
 '{"jsonrpc":"2.0","method":"Article.list", {"limit": 5}],"id":1}');
// this above i beleive dosen't work

                $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($http_code != 200) 
            throw new Exception('Error : Failed to get orders');

        return $data['result'];
    }
}
?>

In this POSTFIELD where i write "// this below i beleive dosen't work" i want to put this properly

Article.list(true, {"limit": 5}) 

see the documentation http://api.textalk.se/webshop/interface/TryIt/

In my other PHP file that looks like this i would like the result to output

<?php
require_once('the_file_with_the_class_TextalkApi.php');

$textalk = new TextalkApi();
//Working demo credentials
$admin_auth_token = $textalk->AdminLogin('pon85x@gmail.com', 'abc123');
$webshop_id = '77140';

$arts = $textalk->GetArts($admin_auth_token, $webshop_id);
echo '<pre>';print_r($arts);echo '</pre>';
var_dump($arts);
?>

But the result is NULL

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
olzonpon
  • 73
  • 9

1 Answers1

0

on line 5, you're not verifying that $username / $password are strings (you should probably thow InvalidArgumentException's if they are)

on line 13, you're not json-encoding $username / $password

on line 14, you're not verifying that the json decode actually succeeded before using it as an array on line 19.

on line 23, you're not verifying the input parameters again, and you're not url encoding $admin_auth_token nor $webshop_id

on line 33, you're sending an invalidly encoded json with a stray ]

on line 36, you're not verifying that the json decode actually succeeded before using it as an array on line 41.

and all over the code, you're not catching any potential curl errors, curl_setopt returns bool(false) if there was an error setting your options, curl_exec returns bool(false) if there was a problem during the transfer, which your code would completely ignore. try using an error-catching wrapper for curl_setopt, like

function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . curl_errno ($ch).'. curl_error: '.curl_error($ch) );
    }
    return true;
}

and also check if curl_exec returns false or not

hanshenrik
  • 19,904
  • 4
  • 43
  • 89