Bitcoin Forum
July 04, 2024, 09:22:49 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need help about PHP and API  (Read 558 times)
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 07:56:58 AM
 #1

Hello,
I have found many things on google but I could not use it because my programmer level is low.
Perhaps could you help me or guide me.

Form an API I get this:

Quote
{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}

How can I have by script

$name="winspiral"
$total=196864
$withdraw=0
$balance=196864

Thanks for your help.

Saovy
Newbie
*
Offline Offline

Activity: 50
Merit: 0


View Profile
September 27, 2017, 09:01:29 AM
 #2

Use json_decode

http://php.net/manual/en/function.json-decode.php

it will turn the json your api returns into an object you can use in any way you see fit.
Thekool1s
Legendary
*
Offline Offline

Activity: 1512
Merit: 1218


Change is in your hands


View Profile
September 27, 2017, 11:36:57 AM
 #3

What are you trying to achieve? I can't help you much if i don't know what you want to do. If you just want to assign data from the json response to the variables, then "Saovy" has pointed you in the right direction, but i doubt that is the only thing you want to do with these variables. Are you building a database of some sort? If you can explain more on what you are trying to achieve, i can only then help you out with a script.
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 12:14:31 PM
 #4

What are you trying to achieve? I can't help you much if i don't know what you want to do. If you just want to assign data from the json response to the variables, then "Saovy" has pointed you in the right direction, but i doubt that is the only thing you want to do with these variables. Are you building a database of some sort? If you can explain more on what you are trying to achieve, i can only then help you out with a script.

I will try to say more:

After doing what Saovy said about I have this

Quote
object(stdClass)#1 (5) { ["success"]=> bool(true) ["name"]=> string(9) "minerpage" ["total"]=> int(932352) ["withdrawn"]=> int(0) ["balance"]=> int(932352) } array(5) { ["success"]=> bool(true) ["name"]=> string(9) "minerpage" ["total"]=> int(932352) ["withdrawn"]=> int(0) ["balance"]=> int(932352) }

But I try to have in my script

echo " balance of $name is $balance";

to see on my site
Balance of minerpage is 932352

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 27, 2017, 12:39:25 PM
Last edit: September 27, 2017, 12:49:54 PM by paraboul
 #5

This is quite dumb, but if you really want to have the named variable exported to the global namespace, you can use extract()

e.g.

Code:
$data = '{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}';
extract((array)json_decode($data), EXTR_SKIP);

echo "Hey $name, your balance is $balance";

Anyway, you shouldn't do this and avoid trashing the global namespace (or current scope), just use a regular object, like :

Code:
$obj = json_decode($data);
echo "Hey $obj->name, your balance is $obj->balance";
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 01:27:12 PM
 #6

This is quite dumb, but if you really want to have the named variable exported to the global namespace, you can use extract()

e.g.

Code:
$data = '{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}';
extract((array)json_decode($data), EXTR_SKIP);

echo "Hey $name, your balance is $balance";

Anyway, you shouldn't do this and avoid trashing the global namespace (or current scope), just use a regular object, like :

Code:
$obj = json_decode($data);
echo "Hey $obj->name, your balance is $obj->balance";

Both codes work
but I am not able to replace :
$data = '{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}';
by
 
$data='$file';
why?

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 27, 2017, 01:31:19 PM
 #7

Quote
Both codes work
but I am not able to replace :
$data = '{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}';
by
 
$data='$file';
why?

Because :

1. If $file contains the JSON, it must be either just $file (no quote, or double quote instead of single) or just use $file as the data.
2. If you want to read the data from a file, use something like $data = file_get_contents($path_to_file) (or whatever is the API you get your JSON from)

Anyway, this is very basic programming. If you're planning to deal with other people money, you shouldn't do this yourself.
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 01:43:04 PM
Last edit: September 27, 2017, 01:56:16 PM by winspiral
 #8

Quote
Both codes work
but I am not able to replace :
$data = '{"success":true,"name":"winspiral","total":196864,"withdrawn":0,"balance":196864}';
by
 
$data='$file';
why?

Because :

1. If $file contains the JSON, it must be either just $file (no quote, or double quote instead of single) or just use $file as the data.
2. If you want to read the data from a file, use something like $data = file_get_contents($path_to_file) (or whatever is the API you get your JSON from)

Anyway, this is very basic programming. If you're planning to deal with other people money, you shouldn't do this yourself.

Thanks paraboul
it seems it works now.
Do not worry,I do it just for the fun.

Thanks to you all...

you can see my test here:
http://www.winspiral.net/u2cloudmining/minertest.php

Both codes work...thanks again.


paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 27, 2017, 02:17:05 PM
Last edit: September 27, 2017, 02:54:09 PM by paraboul
 #9

Quote

Well, you just leaked your secret key from coin-hive because file_get_contents failed and disclosed the URL you're trying to call (with the secret key embedded).

1. Disable PHP errors on production!
2. We now know we shouldn't trust the thing you're calling 'U2cloudmining'

https://i.imgur.com/pKwxIK7.jpg
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 02:54:23 PM
 #10

Well, you just leaked your secret key from coin-hive because file_get_contents failed and disclosed the URL you're trying to call (with the secret key embedded).

1. Disable PHP errors on production!
2. We now know we shouldn't trust the thing you're calling 'U2cloudmining'



Thanks and how can I do this?
and test it?

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 27, 2017, 03:40:06 PM
 #11

Well, you just leaked your secret key from coin-hive because file_get_contents failed and disclosed the URL you're trying to call (with the secret key embedded).

1. Disable PHP errors on production!
2. We now know we shouldn't trust the thing you're calling 'U2cloudmining'

https://i.imgur.com/pKwxIK7.jpg

Thanks and how can I do this?
and test it?

http://php.net/manual/en/function.error-reporting.php

If it's just for file_get_contents, add a @ in front of the call.
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 04:05:48 PM
 #12

Well, you just leaked your secret key from coin-hive because file_get_contents failed and disclosed the URL you're trying to call (with the secret key embedded).

1. Disable PHP errors on production!
2. We now know we shouldn't trust the thing you're calling 'U2cloudmining'



Thanks and how can I do this?
and test it?

http://php.net/manual/en/function.error-reporting.php

If it's just for file_get_contents, add a @ in front of the call.

I have put

error_reporting(0);

I do not understand your "add a @ in front of the call"

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 27, 2017, 05:59:04 PM
 #13

Quote
I do not understand your "add a @ in front of the call"

In PHP, adding a @ in front of a call disables any error related to this call. But you should globally disable error reporting in your php.ini.
Just read the doc Wink
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 27, 2017, 07:03:16 PM
 #14

Quote
I do not understand your "add a @ in front of the call"

In PHP, adding a @ in front of a call disables any error related to this call. But you should globally disable error reporting in your php.ini.
Just read the doc Wink


I have put this:
Quote
ini_set('display_errors', 'off');
error_reporting(0);
But I do not see a difference.

about the @ I have not yet foud.

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 28, 2017, 07:08:06 PM
 #15

Quote
I do not understand your "add a @ in front of the call"

In PHP, adding a @ in front of a call disables any error related to this call. But you should globally disable error reporting in your php.ini.
Just read the doc Wink


I have put this:
Quote
ini_set('display_errors', 'off');
error_reporting(0);
But I do not see a difference.

about the @ I have not yet foud.

@ just tells PHP not to trigger an error for a particular call. (e.g. when you don't globally disable all the errors)
PrivCoin
Full Member
***
Offline Offline

Activity: 356
Merit: 113


View Profile WWW
September 29, 2017, 03:03:14 AM
 #16

Quote
I do not understand your "add a @ in front of the call"

In PHP, adding a @ in front of a call disables any error related to this call. But you should globally disable error reporting in your php.ini.
Just read the doc Wink


I have put this:
Quote
ini_set('display_errors', 'off');
error_reporting(0);
But I do not see a difference.

about the @ I have not yet foud.

I don't think it's 'off', for PHP I'm pretty sure you want:

ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);

paraboul
Newbie
*
Offline Offline

Activity: 29
Merit: 0


View Profile WWW
September 29, 2017, 08:53:54 AM
 #17

Quote
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);

FWITW, doing this or just error_reporting(0) is the same. It takes a binary flag as arguments. It's like doing 0xFFFF & ~0xFFFF which obviously equals...0.
winspiral (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1026


Free WSPU2 Token or real dollars


View Profile WWW
September 29, 2017, 01:19:17 PM
 #18

Quote
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);

FWITW, doing this or just error_reporting(0) is the same. It takes a binary flag as arguments. It's like doing 0xFFFF & ~0xFFFF which obviously equals...0.

Do not forget than you are talking with an "old noob"

You so far helped me...about the error you have seen...with the secret key:
It was I believe during my test...

So far I so in progress:
http://u2cloudmining.winspiral.net/codshare.php

I had 100 other questions,but in general I try to search and understand by myself first.
And my search are often so "simple" that it is not explained in the "help"...
I'm even till lost about the ' ' and " " I have sometimes to put in the programmes...
Now with "java script" it is not to make me easier...
Fortunately I have time (lol)

And "English" is not my native language...



Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!