I'm working on a task and I've encountered the following exception when trying to reach out PageSpeed APi:
ServerErrorResponseException
Server error response
[status code] 500
[reason phrase] Internal Server Error
[url] http://ift.tt/2ugdYSB
On my local development PC everything works perfect, but on the server it throws this exception. I've tested and curl is enabled and working on the server. When I make a curl request for the same url from the terminal of the server it works but not from the site. I'm using VueJS and Laravel for it. Here is my code:
-
With VueJs I make an axios POST request to my API Controller which loops the selected urls and makes the request to PageSpeed for them:
runTest() { for (var i = 0; i < this.selectedProjectUrls.length; i ++) { axios.post ('/api/'+this.selectedProject.slug+'/' +this.selectedProjectUrls[i].id); } }
Here is my Controller's logic
public function save(Project $project, Url $url) { $pageSpeed = new PageSpeed(); $test['mobile'] = $pageSpeed->getResults($url->name, 'en_US', 'mobile'); $test['desktop'] = $pageSpeed->getResults($url->name, 'en_US', 'desktop'); }
Here is my PageSpeed Service logic:
class PageSpeed
{
/**
* @var string
*/
private $gateway = 'http://ift.tt/2tHMCb0';
/**
* Returns PageSpeed score, page statistics, and PageSpeed formatted results for specified URL.
*
* @param string $url
* @param string $locale
* @param string $strategy
* @param optional array $extraParams
*
* @return array
*
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function getResults($url, $locale = 'en_US', $strategy = 'desktop', array $extraParams = null)
{
if (0 === preg_match('#http(s)?://.*#i', $url)) {
throw new InvalidArgumentException('Invalid URL');
}
$client = new \Guzzle\Service\Client($this->gateway);
/** @var $request \Guzzle\Http\Message\Request */
$request = $client->get('runPagespeed');
$request->getQuery()
->set('prettyprint', false) // reduce the response payload size
->set('url', $url)
->set('locale', $locale)
->set('strategy', $strategy)
->set('key', env('PAGESPEED_INSIGHTS_API_KEY'));
if (isset($extraParams)) {
$query = $request->getQuery();
foreach ($extraParams as $key => $value) {
$query[$key] = $value;
}
}
try {
$response = $request->send();
$response = $response->getBody();
$response = json_decode($response, true);
return $response;
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
$response = $e->getResponse();
$response = $response->getBody();
$response = json_decode($response);
throw new RuntimeException($response->error->message, $response->error->code);
}
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire