使用Google Pagespeed API时,Laravel未定义属性错误

时间:2017-11-14 11:51:24

标签: php laravel api laravel-5 pagespeed

我是使用laravel的初学者。对于我的webapp,我想使用Google API PageSpeed。访问/ pagespeed时,它给了我这个错误:ErrorException(E_NOTICE) 未定义的属性:stdClass :: $ ruleScore

此外,我不确定我是否做出了正确的回报。我只是想在/ pagespeed页面上显示测量数据。

无法找到我的问题的答案,我希望你能提供帮助。

我使用了来自https://gist.github.com/typhonius/6259822/revisions的pagespeed.php代码并将其放入我的PagespeedController:

if value == 'failed':
    result_prop = 'Result'
    result = myservice_proxy.Get('org.freedesktop.systemd1.Service',
                                 result_prop,
                                 dbus_interface='org.freedesktop.DBus.Properties')
    print(result)

这是在我的观察文件夹中:pagespeed.blade.php:

public function pageSpeed(){
$url = 'https://www.facebook.com';
$key = 'my API key';
// View https://developers.google.com/speed/docs/insights/v1/getting_started#before_starting to get a key
$data = json_decode(file_get_contents("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=$url&key=$key"));
$dat = $data->formattedResults->ruleResults;
foreach($dat as $d) {
  $name = $d->localizedRuleName;
  $score = $d->ruleScore;
  print "\nTest: " . $name . "\n";
  print "Score: " . $score . "\n";
  if ($score != 100) {
    if (isset($d->urlBlocks[0]->header->args)) {
      $advice_header = replace_placeholders($d->urlBlocks[0]->header->format, $d->urlBlocks[0]->header->args);
    }
    else {
      $advice_header = $d->urlBlocks[0]->header->format;
    }
    print "Advice: " . $advice_header . "\n";
    foreach ($d->urlBlocks[0]->urls as $url) {
      $advice = replace_placeholders($url->result->format, $url->result->args);
      print $advice . "\n";
    }
  }
};
function replace_placeholders($format, $args) {
  $i = 1;
  foreach ($args as $arg) {
    $format = str_replace("\$" . $i, "$arg->value", $format);
    $i++;
  }
  return view('pagespeed')->with('format');
}

这是我在web.php中的路线:

@extends('layouts.app')
@section('content')
<h1>Page Speed</h1>
@endsection

1 个答案:

答案 0 :(得分:0)

更改此

$score = $d->ruleScore;

$score = $d->score;