Also if you are just doing this for testing, it might be worth looking into some friendly tool like httpie. [1] (Otherwise you'll probably want to use an existing library; [2] there are all kinds of edge cases in handling the API which take a bit of effort to reimplement.)

[1] https://httpie.org/
[2] https://www.mediawiki.org/wiki/API:Client_code#PHP