r/PHPhelp • • 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.

3 Upvotes

12 comments sorted by

2

u/MateusAzevedo Feb 11 '25

What is the proper process to integrate a third party api

a function that will take variables and make a curl request

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)

1

u/Available_Canary_517 Feb 11 '25

I am just asking for process you do as developer than doing code.

1

u/RaXon83 Feb 11 '25

You should create a diagram with conditions, based on the condition, you do a step, it might go to a previous point, or the next step in the process (diagram)

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.

1

u/CrazySurround4892 Feb 13 '25

What about when you need to make a lot of requests, curl_multi?

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:

  • You have a TODO or a Ticket to do this work
  • You start with the production branch in git (master/main)
  • You make a new feature branch, ideally with a name that ties it to your ticket or TODO
  • You work away as long as you need to, making commits and testing along the way
  • Ideally you write unit tests for this new code
  • When it's been tested and works, then you do a pull request, or possibly you merge or rebase into the production branch
  • Do final tests and deploy

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

1

u/Available_Canary_517 Feb 11 '25

If I only need to use the API for storing data (e.g., sending data to an endpoint without complex logic), would a simple function be enough, or do you still recommend a class-based approach? In what cases do you think a class would be beneficial even for basic API interactions?