Mautic Community Forums

how to connect mautic through its Rest api from custom php or curl

I am developing a plugin in some php framework.From which i need to integrate mautic and get the certain data from mautic with this plugin.I have read the developer documentation of mautic, i have installed mautic locally and i am unable to authenticate with oauth1a also with basic authentication. My basic authentication code redirects me to login page and in case of oath1a it is giving me the error Unauthorized access to URL.

Here is my code for basic authentication :

Code:
<?php $login = 'admin'; $password = '123456'; $url = 'http://127.0.0.1/mautic/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Basic " . base64_encode($login . ":" . $password) )); //curl_setopt($ch, CURLOPT_USERPWD, "$login:$password"); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result = curl_exec($ch); print_r(curl_exec($ch)); curl_close($ch); //echo($result);

And through oath1a the code is
Code:
<?php $url = 'http://127.0.0.1/mautic/oauth/v1/request_token'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: OAuth oauth_callback="http%3A%2F%2F127.0.0.1%2Fmautic%2Fs%2Fcontacts", oauth_consumer_key="1o1tl7zmuatcwkcw0k84wo0wgwsow4kwsgskwow044g0s0soow", oauth_nonce="4dp2mp6v86qsgc4844ksskkkw8k4s8ksg4gos84g84osookcgw", oauth_signature="GENERATED_REQUEST_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318467427", oauth_version="1.0"' )); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result = curl_exec($ch); print_r($result); //print_r(curl_exec($ch)); curl_close($ch);


And this oath is giving me error
Code:
{ "error": { "message": "Unauthorized access to requested URL: /mautic/oauth/v1/request_token", "code": 403 } }

I am developing a plugin in some php framework.From which i need to integrate mautic and get the certain data from mautic with this plugin.I have read the developer documentation of mautic, i have installed mautic locally and i am unable to authenticate with oauth1a also with basic authentication. My basic authentication code redirects me to login page and in case of oath1a it is giving me the error Unauthorized access to URL.
Here is my code for basic authentication :

<?php
$login = 'admin';
$password = '123456';
$url = 'http://127.0.0.1/mautic/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array(
        "Authorization: Basic " . base64_encode($login . ":" . $password)
    ));
//curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
print_r(curl_exec($ch));
curl_close($ch);
//echo($result);

And through oath1a the code is

<?php $url = 'http://127.0.0.1/mautic/oauth/v1/request_token'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: OAuth oauth_callback="http%3A%2F%2F127.0.0.1%2Fmautic%2Fs%2Fcontacts", oauth_consumer_key="1o1tl7zmuatcwkcw0k84wo0wgwsow4kwsgskwow044g0s0soow", oauth_nonce="4dp2mp6v86qsgc4844ksskkkw8k4s8ksg4gos84g84osookcgw", oauth_signature="GENERATED_REQUEST_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318467427", oauth_version="1.0"' )); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result = curl_exec($ch); print_r($result); //print_r(curl_exec($ch)); curl_close($ch);

And this oath is giving me error

{ "error": { "message": "Unauthorized access to requested URL: /mautic/oauth/v1/request_token", "code": 403 } }

That’s ok @muhammadwasim glad you sorted it out. To answer your question, if you use BasicAuth, you need to include the header for each call. Therefore this should only be used on a secure connection. Basically OAuth1 is broken for complex data so you only have OAuth2 which as an API is problematic unless you manage the tokens properly. The API doco @whoami pointed to is the best reference point except there is no token management provided.

I think that’s overhead when you set authorization header in every api call … What you suggest @MarkLL i need data from mautic i-e contacts,ip location,points trigger etc in realtime so that i can show them in my program.Dont you think token based approach is more reliable … I have already read the documentation but its quite vague… Can you give me example of oath1a or oath2 examples of mautic integration ?

https://www.mautic.org/blog/developer/how-to-use-the-mautic-rest-api/
https://github.com/mautic/api-library

@whoami I have already read the above documentation but not successful. Can you give me example of oath1a authentication that connects with mautic and receive access token??

Hey @muhammadwasim check out the docs at https://developer.mautic.org/#rest-api

The trouble with your BasicAuth example is that you are not pointing to a valid endpoint. To demonstrate, using curl from the command line this works for me.

curl -v -H “Authorization: basic YXBpdXNlcjpwYXNzd29yZDE=” http://127.0.0.1/mautic/api/contacts

The trick is to add /api/ to your install url and then the endpoints which are shown in the doco I pointed to.

Thanks @MarkLL i checked it it works for me but i have a question if i had to do multiple call to mautic for different data i would have to do authorization with all calls… or i could save some token from basic authentication ???

Hey @muhammadwasim there will always be authorization overhead! Whether you choose BasicAuth, OAuth1a or OAuth2 is completely up to you. They all have there pros and cons. The documentation (https://developer.mautic.org/) is actually very comprehensive, but it takes some knowledge and experience to work through it. Every time I re-read it I get more from it.