API Connection - Migrate C# example to PHP (Wordpress)

Viewed 60

I need to connect to an API. All examples that I could find use Tokens which I can send to each transaction I desire.

Accordingly to the supplier documentation, I couldn't find anything related to tokens.

The problem is when I connect, using curl or wp_remote_post() I don't have an 'connected' object to keep doing the transactions that I need.

Bellow is how it is done in C#. I need some directions in what objects I have to use and create the same functionality in php. Thanks

Connection Class:

public class RestService: IDisposable {
    private readonly HttpClient _httpClient;
    private readonly string _acumaticaBaseUrl;
    public RestService(
    string acumaticaBaseUrl, string userName, string password, string company, string branch, string locale) {
        _acumaticaBaseUrl = acumaticaBaseUrl;
        _httpClient = new HttpClient(
        new HttpClientHandler {
            UseCookies = true,
            CookieContainer = new CookieContainer()
        }) {
            BaseAddress = new Uri(acumaticaBaseUrl + "/entity/Default/6.00.001/"),
            DefaultRequestHeaders = {
                Accept = {
                    MediaTypeWithQualityHeaderValue.Parse("text/json")
                }
            }
        };
        //Log in to MYOB Advanced 
        _httpClient.PostAsJsonAsync(
        acumaticaBaseUrl + "/entity/auth/login", new {
            name = userName,
            password = password,
            company = company,
            branch = branch,
            locale = locale
        }).Result.EnsureSuccessStatusCode();
    }
    void IDisposable.Dispose() {
        _httpClient.PostAsync(_acumaticaBaseUrl + "/entity/auth/logout", new ByteArrayContent(new byte[0])).Wait();
        _httpClient.Dispose();
    }
}

////////////////

//Data submission 
public string Put(string entityName, string parameters, string entity) {
    var res = _httpClient.PutAsync(_acumaticaBaseUrl + "/entity/Default/6.00.001/" + entityName + "?" + parameters, new StringContent(entity, Encoding.UTF8, "application/json")).Result.EnsureSuccessStatusCode();
    return res.Content.ReadAsStringAsync().Result;
}
}
0 Answers
Related