hello, i need some advice for using API for PHP…
i try in URL with :
> http://xxx.xxx.xxx.xxx/api/resource/User
if im not login before that api will return :
> {"data":[]}
then i try if i login first in program, then i try again in URL, and i got
this as return :
> {
> "data":[
> {"name":"Administrator"},
> {"name":"a@xxx.com"},
> {"name":"b@xxx.com"}
> ]
> }
and the problem is,
when i try to build syntax inside file php, i can login (success) but when i try to get data or post data,
it will error (feel like i didnt get any permission to do that / i never login before so i cannot access the data)
this is my syntax in index.php :
> <?php
> // =============login=================
> $url= 'http://xx.xx.xx.xx/api/method/login';
> $ch = curl_init();
> curl_setopt( $ch, CURLOPT_URL, $url );
> curl_setopt( $ch, CURLOPT_POST, true );
> curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
> curl_setopt($ch, CURLOPT_VERBOSE, true);
> curl_setopt( $ch, CURLOPT_POSTFIELDS,array(
> 'usr'=>'administrator',
> 'pwd'=>'admin'
> ));
> $result = curl_exec($ch);
> echo $result;
> echo '<br>';
> echo '<br>';
> // ===============get data======================
> $url = 'http://xx.xx.xx.xx/api/resource/User';
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> curl_setopt($ch, CURLOPT_URL,$url);
> $result=curl_exec($ch);
> var_dump(json_decode($result, true));
> echo '<br>';
> echo '<br>';
> // ===================create data=====================
> $url = 'http://xx.xx.xx.xx/api/resource/User';
> $ch = curl_init();
> curl_setopt( $ch, CURLOPT_URL, $url );
> curl_setopt( $ch, CURLOPT_POST, true );
> curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
> curl_setopt( $ch, CURLOPT_POSTFIELDS,array(
> 'data' => '{"name":"Mambo"}'
> ));
> $result = curl_exec($ch);
> echo $result;
> ?>
and this is what i got in my browser :
success in login but cannot get the data User and cannot create new User
seem like i didnt get permission to access
any idea how to solve this? is my syntax wrong? (@.@)
i also try using this 1 :
> https://github.com/tmimori/FrappeClient-PHP
but i got error about cookie.txt