[SOLVED] Fetch a wallet in PHP with Curl (barebone), how do I use my server at 'usemoralis.com'?

I’m trying to fetch a wallet from Moralis, to display the NFTs held in it.

Limitation: I can’t use composer (and hence not the Web3 SDK), but I don’t need all its functionality.

The code below returns a wallet, but since it’s the development server, it stops working.

I like to use this code now on my own server: “https://txvxu97okj3q.usemoralis.com:2053

How do I do this with Curl in PHP?

Especially: how do I set the Application ID and Master Key?


<?php

$address = '0x837ca59C72bDCc902Ae1aabf9B5E49caa628cd80'; // Aidan Cullen's Wallet

$url = 'https://deep-index.moralis.io/api/v2/'.$address.'/nft';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-API-Key: <Key-Was-Here>'));

$res = curl_exec($ch);

curl_getinfo($ch, CURLINFO_HTTP_CODE);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$responsecode = curl_getinfo($ch,CURLINFO_RESPONSE_CODE);

curl_close($ch);

echo "<p>Code = $httpcode<BR />Response = $responsecode</p>";

$data = json_decode($res,true);

echo '<pre>';

print_r($data);

echo '</pre>';

?>

you don’t need the server url for this request only that

I don’t know why it doesn’t work, do you have any error?

I get CURLINFO_HTTP_CODE = 200 and
CURLINFO_RESPONSE_CODE = 200
But the body is empty…

maybe you need to close this one later, I don’t know how it should be in PHP

Is it the body that’s empty or the data/results that is empty? Try another address.

No, that’s not it, the result from the curl request is passed to $res
Also, the code did work and stopped working.

The body is empty.

The wallet address was correct, since the code was working, but stopped working…

I think also data comes back (noting: no Tokens) if the wallet is empty, or the wallet address is wrong.

Thanks for clarifying. Your exact code is working for me in a sandbox with my own key, I see a printed response showing a total of 226.

1 Like

A dead X-API-Key - that makes sense. :roll_eyes:
I’ll have a look.

It’s getting stranger…

Checked the API Key, it’s the key of my private server (instance) - so that is correct.

Checked with Moralis helpdesk - the key is not locked or throttled.

He reset my server, that didn’t help.

So… Technical support team is going to look into it.

try to make the request in web3api interface: https://admin.moralis.io/web3Api

I just uploaded the scripts to an online test environment (on the server where I’m going to use them) And… they are working perfectly fine there…

Seems I have crashed something in my local development environment (XAMPP)…

So, going to do a reinstall and see where I am at then.
Thank you for the help so far!

(And if someone knows what it might be that I crashed: any suggestions welcome).

Allright… So there is a bug in PHP 7 with the Curl library.

It works, but can randomly stop working. Triggers to crash it vary per use case.
This bug was triggered in my installation when I started to use Curl in combination with a MySQL database connection.

https://bugs.php.net/bug.php?id=78891

I updated my development environment to PHP 8.1 and everything’s working fine now.
Thanks for your support!

2 Likes