Dynamic loading data in mailchimp API - php

Mailchimp code:
$api = new MCAPI ('xxxxxxxxxxxx-us11')
This is my code:
$api = new MCAPI ('<?php echo $member[chimpapi]; ?>')
The second for the listID:
$api = new MCAPI ('<?php echo $member[chimplist]; ?>')
Of course this is not good not working, any suggestions?

I am not familiar with the Mailchimp API, but this piece of code:
$api = new MCAPI ('xxxxxxxxxxxx-us11');
is already PHP code, there is no need to include the php tags in it:
$api = new MCAPI ($member['chimpapi']);
and
$api = new MCAPI ($member['chimplist']);

Related

Google Search Console API, URL Inspection

I want to use the Google Search Console API to check if an url is indexed.
$client = new Google_Client();
$client->setAuthConfig( json_decode( $json, true ));
$scopes = ['https://www.googleapis.com/auth/webmasters','https://www.googleapis.com/auth/webmasters.readonly'];
$client->addScope($scopes);
$searchConsole = new Google_Service_SearchConsole($client);
$urlRequest = new \Google_Service_SearchConsole_InspectUrlIndexRequest();
$urlRequest->setInspectionUrl($url);
$urlRequest->setLanguageCode('fr');
$urlRequest->setSiteUrl('sc-domain:'.$url);
$urlIndex = new \Google_Service_SearchConsole_Resource_UrlInspectionIndex();
The problem is when I try to create an UrlInspectionIndex to make an inspect() with $urlRequest I get an Exception in the constructor of vendor/google/apiclient/src/Service/Resource.php:66
I don't know what those arguments are. Hope anyone would help me, thank you.
I got it to work like this:
$service = new Google_Service_SearchConsole($client);
$query = new Google_Service_SearchConsole_InspectUrlIndexRequest();
$query->setSiteUrl("https://$domain/");
$query->setInspectionUrl("https://$domain/$page");
$results = $service->urlInspection_index->inspect($query);

Saving Google API Client in PHP

Apologies if I am missing something obvious in this question...
I have an index.php file where I initialise a Google Client, set the scopes, create a service and start pulling data from the Google Tenancy. This works fine.
<?php
require_once 'vendor/autoload.php';
const CLIENT_ID = MY CLIENT ID;
const CLIENT_SECRET = MY SECRET;
const REDIRECT_URI = REDIRECT URI;
session_start();
$client = new Google_Client();
$client->setApplicationName("My Application");
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setScopes('admin scopes');
$adminservice = new Google_Service_Directory($client);
My issue is, I want to get a user's ID from the AdminDirectory API and then pass it to a new page, user.php, with the GET tag id.
So, for example:
<?php
$id = $adminservice->users->get(EMAIL)->id;
?>
<a href = 'user.php?id=<?php echo $id; ?>'>Click Here</a>
How do I transfer my $client variable over to this new user.php page?
I have tried putting the client in $_SESSION['client'] and then extracting it on the new page. I have also tried reinitialising the entire client. Neither seem to work.
Thanks
You have to import the class on the user.php page:
require_once 'vendor/autoload.php';
Next, store the object in the session:
$_SESSION['googleclient'] = $client;
Now to get it on the other page do:
$client = $_SESSION['client'];
If you need anymore help look at this: move object from 1 page to another?

unable to access google spreedsheets values using php

I'm installed
google api for OAuth2 php lib; here: https://code.google.com/p/google-api-php-client/
google spreadsheet api php lib; here: https://github.com/asimlqt/php-google-spreadsheet-client
created the credentials on the API Console using service account https://console.developers.google.com/
Here is the php script I used in combination with the above:
require_once('vendor/autoload.php');
require_once('vendor/composer/autoload_classmap.php');
require_once('vendor/composer/autoload_real.php');
require_once __DIR__.'/vendor/google/apiclient/src/Google/Auth/AssertionCredentials.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
use Google\Spreadsheet\SpreadsheetService;
const G_CLIENT_ID = 'my_client_id';
const G_CLIENT_EMAIL = 'email address';
const G_CLIENT_KEY_PATH = 'key.p12';
const G_CLIENT_KEY_PW = 'notasecret';
$obj_client_auth = new Google_Client ();
$obj_client_auth -> setApplicationName ('newproject');
$obj_client_auth -> setClientId (G_CLIENT_ID);
$obj_client_auth -> setAssertionCredentials (new Google_Auth_AssertionCredentials (
G_CLIENT_EMAIL,
array('https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'),
file_get_contents (G_CLIENT_KEY_PATH),
G_CLIENT_KEY_PW
));
$obj_client_auth -> getAuth () -> refreshTokenWithAssertion ();
$obj_token = json_decode ($obj_client_auth -> getAccessToken ());
$accessToken = $obj_token->access_token;
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('NewSpreadSheet');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle('sssNew Worksheet');
$listFeed = $worksheet->getListFeed();
$row = array('name'=>'John', 'age'=>25);
I receive the following error:
Call to a member function getWorksheets() on a non-object...
I'm not getting how correct it...
Any help n suggestions
I got it... the issue is about i didn't share my spreadsheet... when i shared my spreadsheet with client email then i'm able to insert records into the desired sheets... now i'm able to access every sheet,insert,delete everything...
So, when ever anyone got this problem just share your sheet with your client email.

Crawler + Guzzle: Accessing to form

I am using the php guzzle Client to grab the website, and then process it with the symfony 2.1 crawler
I am trying to access a form....for example this test form here
http://de.selfhtml.org/javascript/objekte/anzeige/forms_method.htm
$url = 'http://de.selfhtml.org/javascript/objekte/anzeige/forms_method.htm';
$client = new Client($url);
$request = $client->get();
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
$response = $request->send();
$body = $response->getBody(true);
$crawler = new Crawler($body);
$filter = $crawler->selectButton('submit')->form();
var_dump($filter);die();
But i get the exception:
The current node list is empty.
So i am kind of lost, on how to access the form
Try using Goutte, It is a screen scraping and web crawling library build on top of the tools that you are already using (Guzzle, Symfony2 Crawler). See the GitHub repo for more info.
Your code would look like this using Goutte
<?php
use Goutte\Client;
$url = 'http://de.selfhtml.org/javascript/objekte/anzeige/forms_method.htm';
$client = new Client();
$crawler = $client->request('GET', $url);
$form = $crawler->selectButton('submit')->form();
$crawler = $client->submit($form, array(
'username' => 'myuser', // assuming you are submitting a login form
'password' => 'P#S5'
));
var_dump($crawler->count());
echo $crawler->html();
echo $crawler->text();
If you really need to setup the CURL options you can do it this way:
<?php
$url = 'http://de.selfhtml.org/javascript/objekte/anzeige/forms_method.htm';
$client = new Client();
$guzzle = $client->getClient();
$guzzle->setConfig(
array(
'curl.CURLOPT_SSL_VERIFYHOST' => false,
'curl.CURLOPT_SSL_VERIFYPEER' => false,
));
$client->setClient($guzzle);
// ...
UPDATE:
When using the DomCrawler I often times get that same error. Most of the time is because I'm not selecting the correct element in the page, or because it doesn't exist. Try instead of using:
$crawler->selectButton('submit')->form();
do the following:
$form = $crawler->filter('#signin_button')->form();
Where you are using the filter method to get the element by id if it has one '#signin_button' or you could also get it by class '.signin_button'.
The filter method requires The CssSelector Component.
Also debug your form by printing out the HTML (echo $crawler->html();) and ensuring that you are actually on the right page.

Adding a Google Calendar using apiClient

I'm finally able to get an Access Token, now I'm very confused as how to add a Calendar using only Google's provided apiClient.
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->description = "What";
$service->calendars->insert($calendar);
This produces:
Error calling POST https://www.googleapis.com/calendar/v3/calendars?key=mykey: (400) Required
Is there some documentation/examples on adding a Calendar? There are a ton of examples, it seems like, for simply adding an Event.
I'm a little closer now, I get
apiServiceException
Error calling POST
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey: (404) Not Found
Using the boilerplate code they gave on the documentation
$calendarListEntry = new CalendarListEntry();
$calendarListEntry->setId("calendarId");
$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
echo $createdCalendarListEntry->getSummary();
Inserting a new calendarEntry in google calendar API v3 returns a 404
How do I change my request URL from
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey
to
https://www.googleapis.com/calendar/v3/calendars
This worked:
// Create new calendar
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->setSummary(Home::model()->count() . '-' . $model->name);
$createdCalendar = $service->calendars->insert($calendar);
You can reference Google Calendar API v1 Developers' Guide.

Categories