r/PHPhelp • u/Available_Canary_517 • Feb 11 '25
What is the proper process to integrate a third party api in exiting php project
So this is a very weird question but i want to know the effective way to integrate a third party api because this is where i have problem- So let's say i need to integrate a instagram third party api which dm people ( purely hypothetical api may not exist)
So what i will do first is create a new project and try to write a function that will take variables needed( i will for now pass them as random test variables) and make a curl request and check if its working.
When i get my function to be working i will create this function in my php project. Then the area of code where i want my code to send dm to insta users i will call this function and if variables are missing then try to fill them into parent function and use in child( my own function i am using inside it).
If that part does not exist so i will directly call my new function passing required variables.
2
u/MateusAzevedo Feb 11 '25
That's basically it. Sometimes APIs also provide PHP SDK/client, then you don't need to use curl directly, but call their functions with your values. What else do you need?
(The rest of the post is really confusing and couldn't make any sense of it)
9
u/ryantxr Feb 11 '25
There’s no one proper way to do anything.
Determine if the api has an SDK. If not, create one.
If I’m making an SDK, I make it as simple as necessary. When Creating an SDK, I use guzzle.
Create cli processes to test the api. Once that works, move that code to where it needs to be.
4
u/oxidmod Feb 11 '25
Also it's better to utilise dependency inversion principle. Instead of your code knowing about API details and using 3-rd party SDK directly, just describe an interface. And then write an adapter between your interface and 3-rd party SDK. All you code now depends on your interface. But switching the adapter you can change what happens without rewriting everything. So, instead of Instagram new adapter could send messages between your users via telegram, mail, etc...
1
u/boborider Feb 11 '25
It is best to use POSTMAN to SIMULATE API requests.
Php codes are provided on the right panel as you provided the required parameters.
Without POSTMAN, it will be hard connecting to delivery systems and payment systems.
1
u/[deleted] Feb 11 '25
The process is:
I'm leaving out CI/CD and things you might do if you have a more sophisticated CI/CD pipeline.
If this isn't possible, because you don't use git, then there's your problem....
1
u/_MrFade_ Feb 12 '25
Use an “Adapter” pattern.
1
u/[deleted] Feb 11 '25
[removed] — view removed comment