• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shippo LIbrary Integration

#1
I am attempting to integrate Shippo's PHP library (https://github.com/goshippo/shippo-php-client) into CodeIgniter4. I have installed the library via composer and as such CI4's autoloader should be autoloading the library.

In looking at my composer.lock file I see that Shippo does not have PSR4 namespacing and is only loaded as a classmap:

Code:
"autoload": {
  "classmap": [
    "lib/Shippo/"
  ]
},


In the CI4 BaseController I have called the class and set the API Key as outlined in the library:
Code:
use Shippo;

class BaseController extends Controller
{
  public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
  {
    Shippo::setApiKey($_SERVER['SHIPPO_KEY']);
  }
}


And then in my Shipments controller I test the function as outlined in the library readme: 
Code:
use Shippo;

class Shipments extends BaseController
{
  public function index($id)
  {
    $fromAddress = array(
      'name' => 'Shawn Ippotle',
      'street1' => '215 Clayton St.',
      'city' => 'San Francisco',
      'state' => 'CA',
      'zip' => '94117',
      'country' => 'US',
      'phone' => '+1 555 341 9393',
      'email' => '[email protected]'
    );
   
    $toAddress = array(
      'name' => 'Mr Hippo"',
      'street1' => 'Broadway 1',
      'city' => 'New York',
      'state' => 'NY',
      'zip' => '10007',
      'country' => 'US',
      'phone' => '+1 555 341 9393',
      'email' => '[email protected]'
    );
   
    $parcel = array(
      'length'=> '5',
      'width'=> '5',
      'height'=> '5',
      'distance_unit'=> 'in',
      'weight'=> '2',
      'mass_unit'=> 'lb',
    );
   
    $shipment = Shippo_Shipment::create(
      array(
        'address_from'=> $fromAddress,
        'address_to'=> $toAddress,
        'parcels'=> array($parcel),
        'async'=> false
      )
    );

    echo "SHIPPO:";
    echo "<pre>";
    print_r($shipment);
    echo "</pre><br><br>";
    exit;
  }

  //--------------------------------------------------------------------

}


I always get an error "Error - Class 'App\Controllers\Shippo_Shipment' not found" and this line highlighted:
Code:
$shipment = Shippo_Shipment::create(

I am assuming this is a namespacing issue, but I cannot figure out the specifics as I am only 3 apps into using CI4.

Any suggestions?
Reply

#2
"use Shippo_Shipment" instead of "use Shippo" in the controller
Reply

#3
THANK YOU! That did it.

I apologize if this is an elementary mistake!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.