Error when using API for PHP

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

You need to keep your session alive between your curl call.
See CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options for curl. Like described in this stackoverflow question.

1 Like

This might help: GitHub - tmimori/How-to-connect-to-ERPNext-v5-API-with-PHP5: How to connect to ERPNext v5 API with PHP 5

@rmehta @NoumirPoutipou
yaay thanks, thats work using CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE
here my new code, maybe someone need it for some inspiration,

> <?php

> define("COOKIE_FILE", "cookie.txt");
> // =============login=================
> $ch = curl_init('http://xx.xx.xx.xx/api/method/login');
> curl_setopt( $ch, CURLOPT_POSTFIELDS,array(
> 'usr'=>'administrator',
> 'pwd'=>'admin' 
> ));
> curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
> curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE); 
> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
> // curl_setopt($ch, CURLOPT_HEADER, true);
> $result = curl_exec($ch);
> echo $result;

> // ===============get data======================
> $ch = curl_init('http://xx.xx.xx.xx/api/resource/User');
> curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
> curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE); 
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> // curl_setopt($ch, CURLOPT_HEADER, true);
> $result=curl_exec($ch);
> echo $result;

> // ===================create data=====================
> $ch = curl_init('http://xx.xx.xx.xx/api/resource/User');
> curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
> curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE); 
> $arr = array('name' => 'mambo1@yahoo.com', 'email' => 'mambo1@yahoo.com', 'first_name' => 'mambo1');
> curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => json_encode($arr)));
> curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

> $result = curl_exec($ch);
> echo $result;

> ?>

hello, i wanna do some like this, get data from erpnext with curl php.
May i know what is the contents of the “cookie.txt” file?

Hi @hani.rizqi !

Normally it is a blank file. the API should be able to write on it. The key to get it working is to have the right write/access permissions.

Another thing that could be useful is to store your credential on a database so, when you are using the curl class you may get those values in a more global way.

This is the class I used like 2 or 1 years ago, maybe could be useful for anyone here:

You may be able to see the implementation in the parent directories,
Cheers! :slight_smile:

1 Like