Issue
I am trying to get lists from mailchimp based on apikey which is stored in database against user_id. I am getting all the lists based in apikey which is stored in my config file in laravel. But here i want to get lists from mailchimp based on api key stored in database.
The code I am using to get all the lists based on apikey from config file is:
public function getLists(Request $request)
{
$request->user()->id;
$result = MailchimpFacade::request( 'lists', ['fields' => 'lists.id,lists.name'] );
$resultArray = ['status' => 1, 'message' => 'Lists appear successfully!', 'dataArray' => $result];
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
Here i want to get lists from mailchimp based on apikey stored in database. You time and help will be highly appreciated!
public function getLists (Request $request)
{
$request->user()->id;
$mc = new MailChimp($request->input('api_key'));
$result = $mc->get('/ping');
return \Illuminate\Support\Facades\Response::json($result, 200);
}
<?php
namespace App\Http\Controllers;
use App\APIKEY;
use DrewM\MailChimp\MailChimp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;
class ApiController extends Controller
{
public function getLists (Request $request)
{
$request->user()->id;
$mc = new MailChimp($request->input('api_key'));
$result = $mc->get('/ping');
return \Illuminate\Support\Facades\Response::json($result, 200);
}
}
public function getLists(Request $request)
{
Auth::user()->id;
$apikey = $request->input('api_key');
DB::table('apikey')
->where('api_key','=', $apikey)
->get();
if($apikey){
$mc = new MailChimp($apikey);
$mailchimp_ping = $mc->get('lists',['fields' => 'lists.id,lists.name'] );
return \Illuminate\Support\Facades\Response::json($mailchimp_ping, 200);
}
else
{ $errorResponse = [
'message' => 'Lists not found!',
'error' => '401'
];
return Response::json( $errorResponse);
}
}
Solution
Use DrewM\MailChimp\MailChimp
instead of Facade
.
use DrewM\MailChimp\MailChimp;
$mc = new MailChimp($apikey);
$result = $mc->get('lists');
print_r($result);
Full Controller, Edit it according to your need-
<?php
namespace App\Http\Controllers;
use DrewM\MailChimp\MailChimp;
class ApiController extends Controller
{
public function authenticateApiKey()
{
$mc = new MailChimp('48cda414960152d55f97a8f44e28ea20-us17');
$mailchimp_ping = $mc->get('lists');
return \Illuminate\Support\Facades\Response::json($mailchimp_ping, 200);
}
}
See this for further details.
Answered By - Sohel0415 Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.