Get all exchange prices of a cryptocurrency from coinbase (PHP) - char

My project is to draw a line chart with chart.js from historical coinbase price data. The result should be similar to Googles crypto chart. I allready have found a simple way to get the live prices of any cryptocurrency, but I don't know how to get the historical data. My project is written in PHP, you can see my live price grabber function below.
/**
* coinbase api call prices
*/
function api_call_price($from, $to) {
$from = (trim(strtoupper($from)));
$to = (trim(strtoupper($to)));
$url = 'curl -s -H "CB-VERSION: 2017-12-06" "https://api.coinbase.com/v2/prices/'.$from.'-'.$to.'/spot"';
$tmp = shell_exec($url);
$data = json_decode($tmp, true);
if ($data && $data['data'] && $data['data']['amount']) {
return (float)$data['data']['amount'];
}
return null;
}
// Usage: api_call_price("BTC", "EUR");

OK, my solution is now to grab the data via cronjob and store it as a JSON string.

Related

bol.com api integration with laravel I need to get product details

here is the link which has been used
https://api.bol.com/retailer/public/Retailer-API/index.html
I have to get order details and single orders with offers but I need to get product details with an image.
I have read all the docs and understood that. I just need the get API of the product, not the post API.
This website gets the product from bol.com API. I need to get product details same like this website.
here is screenshot
anyone here who works on bol.com API.
please need help it's important.
thanks for advance
you need to build scrapper to fetch information from bol.com using crawler
we did as well,
please find on github this repo
"Ultimate Web Scraper Toolkit"
using url you can fetch images here but we fetching more information
function searchByURL($url){
$res = [];
$res['product_url']=$url;
$data = getCode($url);
// Product Title
$rows = $data[1]->Find("h1.page-heading span.u-mr--xs");
foreach ($rows as $row)
{
$res['title'] = strip_tags((string)$row);
}
// Product Image
$rows = $data[1]->Find("div.image-slot img");
foreach ($rows as $row)
{
$res['product_image'] = HTTP::ConvertRelativeToAbsoluteURL($data[0], $row->src);
}
}

Replace Old image with new image in laravel

CONTROLLER
public function updateproduct(Request $request, $id)
{
// dd($request->all());
$request->validate([
'name'=>'required',
'description'=>'required',
'price'=>'required|numeric',
'quantity'=>'required|numeric',
]);
$product = Product::where('id', $id)->first();
if(is_null($product)) {
$product = new Product();
}
$product->name=$request->name;
$product->description=$request->description;
$product->price=$request->price;
$product->quantity=$request->quantity;
if($request->hasfile('images')){
$existingimages = Image::where(['product_id'=> $product->id, 'source'=> 1])->get();
if($existingimages->count() > 0)
foreach($existingimages as $existingimage) {
$filename = public_path().'files/'.$existingimage->name;
if(file_exists($filename)){
unlink($filename);
$existingimage->delete();
}
}
foreach($request->file('images') as $file){
$name = rand(1,9999).'.'.$file->getClientOriginalName().$file->getClientOriginalExtension();
if($file->move(public_path().'/files/', $name)){
$updateImage = Image::firstWhere('product_id', $product->id);
$updateImage->images = $name;
$updateImage->source = 1;
$updateImage->save();
}
}
}
Please check updated question. Request you to correct me if I wrong. Right now it only updates 1 image and other images remains same. please help me with this.
Your code is somewhat confusing, I'm afraid.
Your request appears to allow for multiple images to be uploaded. Here :
if(file_exists($imagePath)){
unlink($imagePath);
}
you look like you're trying to delete any images that already exist, but when you store upload images you're assigning them a random name (which Laravel can already handle for you, by the way, so there's no need to do it yourself) so you're unlikely to be actually deleting them because there'll likely be no file at that location anyway.
Nowhere does your code actually delete any existing images from the database. Essentially what you want to do is :
If the upload has images, retrieve all the existing images for that product.
Delete the physical files for those existing images.
Delete the database entries for those existing images.
Upload your new images and save their details to the database.
Translating that into code means :
if($request->hasfile('images')){
// Delete any existing image files and database entries.
$existingimages = Image::where('product_id', $product->id)->get();
if($existingimages->count() > 0)
foreach($existingimages as $existingimage) {
$filename = public_path().'files/'.$existingimage->name;
unlink($filename);
$existingimage->delete();
}
}
// Carry on with your upload
}
It adds new images because you are using the Image::create method. If I understood correctly, you want to modify the images of your products in the image table.
Try to modify your code like :
$updateImage = Image::firstWhere('product_id', $product->id);
$updateImage->images = $name;
$updateImage->source = 1;
$updateImage->save();

Magento 2.3 graphql get custom attribute option values

I’m pretty frustrated with the graphql api of Magento. How is it not possible to receive the values of a custom attribute. I mean it’s easy to make a custom select-option attribute and making it available through graphql but you are only getting the integer value.
What’s the best procedure to following
1. Query the complete attribute to get the value
2. Extending the graphql schema (this involves a developer every time the client changes something)
3. Am I missing some functionality inside the graphql endpoints to Fix this?
From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file.
/**
* #params string $sku
* this function return all the product data by product sku
**/
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
/**
* #params int $id
* this function return all the word of the day by id
**/
public function getAttributesBySku( $sku ){
$_product = $this->getProductBySku($sku);
$attributes = $_product->getAttributes();// All Product Attributes
$attributes_data = [];
$x=0;
foreach ($attributes as $attribute) {
if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
$attributeLabel = $attribute->getFrontend()->getLabel();
$attributeValue = $attribute->getFrontend()->getValue($_product);
if($attribute->getAttributeCode()=="language"){
$attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
$attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
}
}
$x++;
}
return $attributes_data;
}
This code will return the need
$attribute->getFrontend()->getLabel(); this will return the label and
$attribute->getFrontend()->getValue($_product); this will return the value.
To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?
Hope this will help you.
You can try creating custom graphql attribute with custom resolver.
$product = $this->_productRepository->getById($value['entity_id']);
$data = array();
foreach ($args['fields'] as $fi) {
$att = $product->getResource()->getAttribute($fi);
if (isset($att) && $att) {
if (in_array(
$att->getFrontendInput(),
['multiselect', 'select']
)) {
$data[$fi . '_label'] = $product->getAttributeText($fi);
}
$data[$fi] = $product->getData($fi);
}
}
return json_encode((object) $data);
which should display all provided attributes with their labels.
"sku": "testProduct",
"fabric": 60,
"work": 65,
"dynamicAttributes": "{
"fabric_label":"Pure Silk",
"fabric":"60",
"work_label":"Tanchoi",
"work":"65"
}",
Check out entire module here

Storing Multiple Data in Laravel

I am trying to think of a way that a user can add a type(medical) on the frontend but a verifier has to approve that record? i can't seem to figure out the best solution for this, does anyone have any suggestions on how to make this work? It's getting stored in the medical table but not in Verifier table.
In Simple words, I am using the medicalRecord Controller to stored medical and Verifier details.
'document_id' = the medical_id;
'submitted_by' = who created the record
public function store Request $request, $id)
{
if (auth_user_cannot(Capability::CREATE_DRIVER_MEDICAL_RECORD)) {
return redirect($group['name'] . "/" . $group['userId']);
}
$all = $request->all();
if ($request->hasFile('myfile')) {
$result = upload($request->file('myfile'), 'nonzip', 'Medical');
$all['upload'] = $result['upload'];
$all['uploadId'] = $result['uploadId'];
}
// u'll need to save the medical to the user first, then save the verifier to the medical.
$driverMedical = DriverMedicalRecord::create($all);
// Then for the relation;
$medical = $driverMedical->find($id);
$verificationData = DocumentVerification::create( [
'document_id' => $medical,
'submitted_by'=> Auth::id(),
]) ;
$request->user()->posts()->save($post);
$driverMedical->verifier()->save($verificationData);
set a flag in the same table and change its value when it got verified.
ex: is_verfied it should be 0 when it is not approved when it got approved change it to 1 (this is what i understand from your question)

Caching Eloquent models in Laravel 5.1

I've created an API using Laravel and I'm trying to find out how to cache Eloquent models. Lets take this example as one of the API endpoints /posts to get all the posts. Also within the method there are various filter options such as category and search and also gives the option to expand the user.
public function index()
{
$posts = Post::active()->ordered();
if (Input::get('category')) $posts = $posts->category(Input::get('category'));
if (Input::get('search')) $posts = $posts->search(Input::get('search'));
if ($this->isExpand('user')) $posts = $posts->with('user');
$posts = $posts->paginate($this->limit);
return $this->respondWithCollection($this->postTransformer->transformCollection($posts->all()), $posts);
}
I have been reading up and found in Laravel 4 you could cache a model like this
return Post::remember($minutes);
But I see this has been removed for Laravel 5.1 and now you have to cache using the Cache facade, but is only retrievable by a single key string.
$posts = Cache::remember('posts', $minutes, function()
{
return Post::paginate($this->limit);
});
As you can see, my controller method contains different options, so for the cache to be effective I would have to create a unique key for each option like posts_cagetory_5, posts_search_search_term, posts_category_5_search_search_term_page_5 and this will clearly get ridiculous.
So either I'm not coming across the right way to do this or the Laravel cache appears to have gone backwards. What's the best solution for caching this API call?
As the search is arbitrary, using a key based on the search options appears to be the only option here. I certainly don't see it as "ridiculous" to add a cache to for expensive DB search queries. I may be wrong as I came by this post looking for a solution to your exact problem. My code:
$itemId = 1;
$platform = Input::get('platform'); // (android|ios|web)
$cacheKey = 'item:' . $itemId . ':' . $platform;
$item = Item::find(1);
if( Cache::has($cacheKey) ) {
$result = Cache::get($cacheKey);
} else {
$result = $this->response->collection( $item, new ItemTransformer( $platform ) );
Cache::tags('items')->put($cacheKey, $result, 60); // Or whatever time or caching and tagged to be able to clear the lot in one go...
}
return $result;
I realise that my example has less complexity but it seems to cover all the bases for me. I then use an observer to clear the cache on update.

Resources