Skip to content

Instantly share code, notes, and snippets.

@zero-is-one
Created March 6, 2014 17:17
Show Gist options
  • Save zero-is-one/9394681 to your computer and use it in GitHub Desktop.
Save zero-is-one/9394681 to your computer and use it in GitHub Desktop.
Example using Guzzle with the Shapeways api
<?php
class ShapewaysController extends BaseController {
function buildClient(){
$client = new Guzzle\Http\Client(Config::get('shapeways.base_url'));
$oauth = new Guzzle\Plugin\Oauth\OauthPlugin(array(
'consumer_key' => Config::get('shapeways.consumer_key'),
'consumer_secret' => Config::get('shapeways.consumer_secret'),
'token' => Config::get('shapeways.oauth_token'),
'token_secret' => Config::get('shapeways.oauth_token_secret'),
));
$client->addSubscriber($oauth);
return $client;
}
function requestRequestToken(){
$client = new Guzzle\Http\Client(Config::get('shapeways.base_url'));
$oauth = new Guzzle\Plugin\Oauth\OauthPlugin(array(
'consumer_key' => Config::get('shapeways.consumer_key'),
'consumer_secret' => Config::get('shapeways.consumer_secret'),
));
$client->addSubscriber($oauth);
$response = $client->get("oauth1/request_token/v1")->send();
$authenticationURL = str_replace("authentication_url=", "", urldecode($response->getBody()));
$query = array();
parse_str(parse_url($authenticationURL)["query"], $query );
echo "<h1>Almost done! </h1>";
echo "<br>Temporary request token: ".$query["oauth_token"];
echo "<br>Temporary request token secret: ".$query["oauth_token_secret"];
echo "<br>Verification Pin: Go <a href='$authenticationURL' target='_blank'>here</a>, authorize, and request your verification pin.";
echo "<br><br>Use this to request your access token.";
}
function requestAccessToken($token,$secret,$pin){
$client = new Guzzle\Http\Client(Config::get('shapeways.base_url'));
$oauth = new Guzzle\Plugin\Oauth\OauthPlugin(array(
'consumer_key' => Config::get('shapeways.consumer_key'),
'consumer_secret' => Config::get('shapeways.consumer_secret'),
'token' => $token,
'token_secret' => $secret,
'verifier' => $pin
));
$client->addSubscriber($oauth);
$response = $client->get("oauth1/access_token/v1")->send();
$query = array();
parse_str($response->getBody(), $query);
echo '<h1>Here are your Oauth credentials!</h1>';
echo "<br> Request token: ".$query["oauth_token"];
echo "<br> Request token secret: ".$query["oauth_token_secret"];
}
function getMaterial(){
//this works fine
$response = $this->buildClient()->get("materials/26/v1")->send();
return $response->getBody();
}
function getUpload(){
$filename = "test.zip";
$file = file_get_contents('/Users/victorsigma/Desktop/test.zip');
$data = array(
"fileName" => "$filename",
"file" => rawurlencode(base64_encode($file)),
"hasRightsToModel" => 1,
"acceptTermsAndConditions" => 1,
"isPublic" => 0,
"isForSale" => 1,
);
$data_string = json_encode($data);
try {
$response = $this->buildClient()->post('models/v1', array('Accept' => 'application/json'), $data_string)->send();
die($response);
} catch (Guzzle\Http\Exception\ServerErrorResponseException $e) {
$req = $e->getRequest();
$resp =$e->getResponse();
die($resp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment