Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ApiCommand.php在laravel中是如何工作的?_Php_Laravel - Fatal编程技术网

ApiCommand.php在laravel中是如何工作的?

ApiCommand.php在laravel中是如何工作的?,php,laravel,Php,Laravel,我只是想知道ApiCommand.php在laravel中是如何工作的 这是我的ApiCommand.php <?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; use DB; class ApiCommand extends Command { /** * The name and signature of th

我只是想知道ApiCommand.php在laravel中是如何工作的

这是我的ApiCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

use DB;

class ApiCommand extends Command {
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'api:command';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Command description';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */

public function handle()
{
    include(app_path() . '/includes/HelloAnalytics.php');
    include(app_path() . '/includes/DeepCrawl.php');  
    include(app_path() . '/includes/Uptime.php');
}
<?php

// Load the Google API PHP Client Library.
//require_once '/home/vagrant/apps/pc-reporting-tool/vendor/autoload.php';
include(base_path() . '/vendor/autoload.php');
$results = array();
$otherResults = array();
$analytics = initializeAnalytics();
// there are two websites in google analytics account so $i<2 is used
for($i=0;$i<2;$i++){
$profile[$i] = getFirstProfileId($analytics,$i);

//**** get the property name
$propertyName[$i] = $profile[$i][0];

/* results of a profile is saved with each iteration. The loop iterates
    until all the profile in google analytics account is save */
$results[$i] = getResults($analytics, $profile[$i][1]);
$otherResults[$i] = getOtherResults($analytics, $profile[$i][1]);
}


function initializeAnalytics(){
// Creates and returns the Analytics Reporting service object.

// Use the developers console and download your service account
$KEY_FILE_LOCATION = app_path() . '/includes/GoogleAnalytics.json';

// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);

return $analytics;
}


function getFirstProfileId($analytics,$index) {
// Get the user's first view (profile) ID.

// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();

if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();

// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties
    ->listManagementWebproperties($firstAccountId);

if (count($properties->getItems()) > 0) {
  $items = $properties->getItems();
  $firstPropertyId = $items[$index]->getId();
  //*******************
   $firstPropertyName = $items[$index]->getName();

  // Get the list of views (profiles) for the authorized user.
  $profiles = $analytics->management_profiles
      ->listManagementProfiles($firstAccountId, $firstPropertyId);

  if (count($profiles->getItems()) > 0) {
    $items = $profiles->getItems();

      //***** stores Property name and Profile Id as an array
      $info = [];
      $info[0] = $firstPropertyName;
      $info[1] = $items[0]->getId();
    // Return the first view (profile) ID.
    //return $items[0]->getId();

      return $info;
        } else {
    throw new Exception('No views (profiles) found for this user.');
     }
     } else {
  throw new Exception('No properties found for this user.');
     }
  } else {
  throw new Exception('No accounts found for this user.');
    }
  }

function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'yesterday',      
'ga:users', 
['dimensions'=>'ga:deviceCategory,ga:browser'] */
['dimensions'=>'ga:day'] 
  );
}

function getOtherResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'yesterday',      
  'ga:users,ga:newUsers,ga:sessions,ga:sessionsPerUser,ga:pageviews,ga:pageviewsPerSession,ga:avgSessionDuration,ga:bounceRate'
  );
}

function printResults($results) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {

// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();

// Get the entry for the first entry in the first row.
$rows = $results->getRows();


$sessions = $rows[1][0];

// Print the results.
//print "First view (profile) found: $profileName\n";
//print "Total visits: $sessions\n";
//print "Total users: $users\n";
} else {
   print "No results found.\n";
  }
}
<?php 

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.uptimerobot.com/v2/getMonitors",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "api_key=XXXX",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
$custom_uptime = ($data->monitors[0]->custom_uptime_ratio);
$uptime = explode("-",$custom_uptime);
}

?>

几个问题:为什么要包括HelloAnalytics.php、DeepCrawl.php和Uptime.php?所有的课程都做什么?我认为您可以导入要触发的类和调用方法。您的问题还不够清楚。你打算实现什么?从您发布的代码中,我可以明确建议将include更改为
使用
声明。第二件事是,你确定你没有混淆概念吗?您似乎试图定义console命令,该命令可以手动调用,也可以由队列运行器调用。另一方面,您以某种方式调用它API,这听起来可能像您希望通过API访问它。如果是这种情况,您应该让您的命令收集所有需要的数据并将其存储在某个地方,那么您需要创建API端点,在这里您可以获取存储的数据。您可以包含
HelloAnalytics.php
的内容吗,
DeepCrawl.php
Uptime.php
?@usrnot发现他们使用api从三个不同的站点获取报告数据。您已为您的命令指定了
api:command
签名,因此您只需从命令行运行
php artisan api:command
,即可执行此类的handle方法。