AnsweredAssumed Answered

Fast PURGE API in PHP7

Question asked by Arun Pal on Jan 17, 2018

we are using api-kickstart-master, and able to purge the content from http command line, when we are tryning to use it on web, it says below error:

 

Notice: Undefined index: argv in C:\xampp\htdocs\img\protograph\cli\init.php on line 45

Warning: array_shift() expects parameter 1 to be array, null given in C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php on line 274

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php on line 52

Fatal error: Uncaught TypeError: Argument 1 passed to League\CLImate\Argument\Parser::prefixedArguments() must be of the type array, null given, called in C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php on line 56 and defined in C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php:134 Stack trace: #0 C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php(56): League\CLImate\Argument\Parser->prefixedArguments(NULL) #1 C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Manager.php(226): League\CLImate\Argument\Parser->parse(NULL) #2 C:\xampp\htdocs\img\protograph\cli\init.php(45): League\CLImate\Argument\Manager->parse(NULL) #3 C:\xampp\htdocs\img\protograph\ccu_v3.php(32): require_once('C:\\xampp\\htdocs...') #4 {main} thrown in C:\xampp\htdocs\img\protograph\vendor\league\climate\src\Argument\Parser.php on line 134

 

our code is as below;

<?php
/**
 * Copyright 2016 Akamai Technologies, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Sample client for CCU
 * Note that in order for this to work you need to provision credentials
 *  specifically for CCU - you cannot extend existing credentials to add
 * CCU as it's managed under "CCU" in the API credential system.
 *
 * Configure->Organization->Manage APIs
 * Select "CCU APIs"
 * Create client collections/clients
 * Add authorization
 *
 * Put the credentials in ~/.edgerc as demonstrated by api-kickstart/sample_edgerc
 */
namespace Akamai\Open\Example;
ini_set('register_argc_argv','on');
define('STDIN', fopen('php://stdin', 'r'));
require_once __DIR__ . '/cli/init.php';

class CcuClient
{
    /**
     * @var \Akamai\Open\EdgeGrid\Client
     */
    protected $client;

    public function __construct()
    {
        $this->client = \Akamai\Open\EdgeGrid\Client::createFromEdgeRcFile('default');
    }

    public function postPurgeRequest($hostname, $objects)
    {
        $purge_body = [
            'hostname' => $hostname,
            'objects' => $objects,
            "action"=>"remove",
            "type"=>"arl",
            "domain"=>"production"
        ];

        $response = $this->client->post('/ccu/v3/invalidate/url', [
            'body' => json_encode($purge_body),
            'headers' => ['Content-Type' => 'application/json']
        ]);
        return $response;
    }
}
if ( ! empty( $_REQUEST['purge'] ) && ! empty( $_REQUEST['purgeurl'] ) ) {
    $ccu = new CcuClient();
    try {
        $objects = [
            str_replace( array( 'https://protograph.indianexpress.com', 'http://protograph.indianexpress.com', 'protograph.indianexpress.com' ), '', trim( $_REQUEST['purgeurl'] ) )
        ];

        //$purge = $ccu->postPurgeRequest('akamaiapibootcamp.com', $objects);
        $purge = $ccu->postPurgeRequest('protograph.indianexpress.com', $objects);
        $response = json_decode($purge->getBody());
        header( 'Location:http://localhost/protograph/ccu_v3.php?msg=Success (' .$purge->getStatusCode(). ') == Estimated Purge Time: ' .$response->estimatedSeconds. 's' );
    } catch (\GuzzleHttp\Exception\ClientException $e) {
        header( 'Location:http://localhost/protograph/ccu_v3.php?msg=An error occurred: ' .$e->getMessage(). ' == Please try again with --debug or --verbose flags.' );
    }
}
if ( ! empty( $_REQUEST['msg'] ) ) {
    echo $_REQUEST['msg'];
}
?>
<form>
<input type="text" value="" name="purgeurl">
<br clear="all">
Example : http://protograph.indianexpress.com/Assets/quiz-nitish-kumar.png
<br clear="all">
Enter only : /Assets/quiz-nitish-kumar.png
<br clear="all">
<input type="submit" value="Purge" name="purge">
</form>

Outcomes