Connect Google cloud storage with Google app engine - codeigniter

How can I connect My Codeigniter Project in Google App Engine with Google Cloud Storage?
Any ideas?

You can use the PHP Client Library [1]. Here you hace an example to upload a file to a bucket:
use Google\Cloud\Storage\StorageClient;
/**
* Upload a file.
*
* #param string $bucketName the name of your Google Cloud bucket.
* #param string $objectName the name of the object.
* #param string $source the path to the file to upload.
*
* #return Psr\Http\Message\StreamInterface
*/
function upload_object($bucketName, $objectName, $source)
{
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
[1] https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-php

Related

Unable to post data

Hello, it is a very important project for me. I wrote an api and I want to pull data from 2 different sites with this api. While you can pull healthy data from one site, empty results are returned due to captcha from the other site. Now I scraped data from the same site before using pyhton selenium, bs4, but I can't scrape it with laravel api. I'm testing this info from postman.
The solution of the captcha is to combine my pyhton file, which I have provided, with my laravel api file, if possible, by converting the captcha to audio and then to text, which skips this part.
Another method is to write a pyhton service that can work with the captcha(pyhton) file.
The last and actually the most useful method for me if I can provide action is to skip captcha with my laravel service without entering the captcha event. I tried skipping by changing the cookie, but it was a very tiring and unhealthy method. I want to trick some kind of captcha here.
<?php
namespace App\Services\Mde;
use App\Core\ServiceResponse;
use App\Interfaces\IPricePredictionService;
class PricePredictionService extends BaseMDeService implements IPricePredictionService
{
/**
* #param mixed $brand
* #param mixed $model
* #param mixed $kilometerFrom
* #param mixed $kilometerTo
* #param mixed $yearFrom
* #param mixed $yearTo
* #param mixed $fuelTypes
* #param mixed $gearBoxes
* #param mixed $powerFrom
* #param mixed $powerTo
*
* #return ServiceResponse
*/
public function getByParameters(
$brand,
$model,
$kilometerFrom,
$kilometerTo,
$yearFrom,
$yearTo,
$fuelTypes,
$gearBoxes,
$powerFrom,
$powerTo
): ServiceResponse
{
$endpoint = $this->baseUrl . '&ms=' . $brand . '%3B' . $model;
$priceList = [];
for ($pageCounter = 1; $pageCounter <= 50; $pageCounter++) {
$parameters = [
'ml' => $kilometerFrom,
'ml%3A' => $kilometerTo, // duzelt
'fr' => $yearFrom, // duzelt
'fr%3A' => $yearTo, // duzelt
'fuel' => implode(' ', $fuelTypes), //ft=DIESEL
'gear' => implode(' ', $gearBoxes), //tr=MANUAL_GEAR
'powertype' => 'kw',
'pw' => $powerFrom, // duzelt
'pw%3A' => $powerTo, // duzelt
'page' => $pageCounter,
];
$response = $this->client->get($endpoint . '?' . http_build_query($parameters), [
'headers' => [
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
//'Accept' => 'application/json',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept-Language' => 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' //,
],
]);
$clean1 = str_replace(["\n", "\t", "\r", " "], null, $response->getBody()->getContents());
$clean2 = str_replace(["""], null, $clean1);
$clean3 = preg_replace('~{(.*?)}~', null, $clean2);
$cleanResult = preg_replace('~{(.*?)}~', null, $clean3);
preg_match_all('~<a class="link--muted no--text--decoration result-item" (.*?)</a>~', $cleanResult, $articles);
//return $lists; // bos geliyor.
if ($articles[1]) {
foreach ($articles[1] as $article) {
preg_match('~<span class="h3 u-block" .*?>(.*?)</p>~', $article, $priceObject);
$priceWithCurrency = str_replace(',-', null, $priceObject[1]);
$priceWithoutCurrency = explode(' ', $priceWithCurrency)[1];
$price = intval(str_replace('.', null, $priceWithoutCurrency));
$priceList[] = $price;
}
}
}
$averagePrice = array_sum($priceList) / count($priceList);
return new ServiceResponse(
true,
'Average price calculated successfully.',
200,
intval($averagePrice)
);
}
}
I don't insist that these methods are just the ones that come to my mind, I want to proceed in this way, I just want to provide fast and practical transactions whenever possible. I'm sorry if I said something illogical and wrong. But I desperately need the ideas and guidance you will convey to me. Thank you from now. I'm also adding my api codes in case you want to take a look.
The controller class is below.
```
<?php
namespace App\Http\Controllers\Api\M;
use App\Core\Controller;
use App\Core\HttpResponse;
use App\Http\Requests\Api\M\PricePredictionController\CheckRequest;
use App\Interfaces\IPricePredictionService_M;
class PricePredictionController extends Controller
{
use HttpResponse;
/**
* #var $mService
*/
private $mService;
public function __construct(IPricePredictionService_M $mService)
{
$this->mService = $mService;
}
/**
* #param CheckRequest $request
*/
public function check(CheckRequest $request)
{
$response = $this->mService->getByParameters(
$request->brand_and_model,
$request->kilometerFrom_kilometerTo,
$request->yearFrom_yearTo,
$request->fuelTypes ?? [],
$request->gearBoxes ?? [],
$request->powerFrom_powerTo,
$request->country,
$request->bodyType,
$request->doors
);
return $this->httpResponse(
$response->getMessage(),
$response->getStatusCode(),
$response->getData(),
$response->isSuccess()
);
}
}
> The interfaces class is below.
```
<?php
namespace App\Interfaces;
use App\Core\ServiceResponse;
interface IPricePredictionService_M
{
/**
* #param mixed $brand_and_model
* #param $kilometerFrom_kilometerTo
* #param mixed $yearFrom_yearTo
* #param mixed $fuelTypes
* #param mixed $gearBoxes
* #param mixed $powerFrom_powerTo
* #param mixed $country
* #param mixed $bodyType
* #param mixed $doors
*
* #return ServiceResponse
*/
public function getByParameters(
$brand_and_model,
$kilometerFrom_kilometerTo,
$yearFrom_yearTo,
$fuelTypes,
$gearBoxes,
$powerFrom_powerTo,
$country,
$bodyType,
$doors
): ServiceResponse;
}

Curl returning null response when run from artisan command for cron job

Trying to run an API which will give me the updated temperature and humidity values but the curl function is not working as it gives a NULL reponse and throws error. running the from the terminal to test it
code:
class updateTempHumHourly extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'update:temphum';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update temperature and humidity readings hourly';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$options = array(
'cluster' => 'ap2',
'useTLS' => true
);
$pusher = new \Pusher\Pusher(
'osdifnerwion4iownfowinf',
'dofhewofhewofnowenfid',
'7asdkland',
$options
);
$thinkers = t::where('user_id', '!=' , NULL)->where('thinker_status',1)->get();
foreach($thinkers as $t)
{
$temp = [
'action'=>'list_slave',
'mac'=> $t->thinker_MAC,
'type' => 'all',
'appkey' => 'nope'
];
json_encode($temp);
$response = Curl::to('http://103.31.82.46/open/open.php')
->withContentType('application/json')
->withData($temp)
->asJson(true)
->withHeader('Postman-Token: d5988618-676e-430c-808e-7e2f6cec88fc')
->withHeader('cache-control: no-cache')
->post();
foreach($response['slaves'] as $s)
{
if(array_key_exists("temp",$s) && array_key_exists("hum",$s))
{
$slave = sd::where("connected_thinker_MAC",$response['mac'])->where("device_id",$s['slave_id'])->first();
$slave->temperature = $s['temp'];
$slave->humidity = $s['hum'];
$slave->save();
$array = [];
$array['temperature'] = $s['temp'];
$array['humidity'] = $s['hum'];
$array['id'] = $s['slave_id'];
$pusher->trigger($t->user_id."-channel","s-event",$array);
\App\helper\log::instance()->tempHumLog($s);
}
}
}
}
}
the foreach loop throws an error that $response is equal to null. the curl function is not working from here but working fine regularly. help. i need to run this task every hour to get the average temperature and humidity.
Since you said that your code works somewhere that means that there is something wrong with your server and not the code itself probably (since it's a CURL issue).
Inside your php.ini file there is this line : extension=php_curl.dll to enable curl. Uncomment it on your server that you have the crons to make curl work. This should solve your problem.
Testing localy the api you provide us returns a 500 but if you say that it works for you i assume that this is just an example and the problem is in CURL itself.

yii2-websocket issue for getting online users list

I am using this package for a chat application. I am facing issue to get the online users list. There is a way suggested by someone I tried that but no success.
Code below for getting the online users list.
/**
* Subscribe to messages
*
* #param ConnectionInterface $client
* #param string $msg
*/
public function commandSubscribe(ConnectionInterface $client, $msg)
{
$request = #json_decode($msg, true);
$client->talkId = $request['talk_id'] ?? null;
$client->userId = $request['user_id'] ?? null;
$this->clients = $client;
foreach ($this->clients as $key=>$chatClient) {
$onlineUsers[] = $chatClient->name;
}
$client->send( json_encode(['onlineUsers'=> $onlineUsers, 'room'=>$client->talkId, 'user' =>$client->userId ,'message'=> 'User added to room']) );
}
I get the below response:
Response:{"onlineUsers":{},"room":"provider","user":"hassan","message":"User added to room"}

Add module to subpage in Joomla

I have a module which I add from admin panel to some subpage. After that some subpages show properly content with this module but some subpages after click on it open blank, white page with no content inside. I don't know what caused that problem. Why some subpages with this module work properly and some show blank page?
This is what I see on page:
Fatal error: Cannot redeclare class ModProductsMenuHelper in /opt2/data-dev/modules/mod_products_menu/helper.php on line 15
Thank you for help!
This is my code
<?php
/**
* Slajder class for Hello World! module
*
* #package Joomla.Tutorials
* #subpackage Modules
* #link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
* #license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
class ModProductsMenuHelper
{
/**
* Retrieves the hello message
*
* #param array $params An object containing the module parameters
*
* #access public
*/
public function getProducts($params)
{
$lang = JFactory::getLanguage();
$langTag = $lang->getTag();
$app = JFactory::getApplication();
$isSMB = $app->get('isSMB');
$parentMenuId = $langTag == 'pl-PL' ? 107 : 103;
$results = $this->getChildren($parentMenuId, $langTag);
return $results;
}
private function getChildren($parentId, $langTag){
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select(array('id', 'title', 'path', 'alias'))
->from($db->quoteName('#__menu'))
->where("(language = '*' OR language= ".$db->quote($langTag).") AND published = 1 AND parent_id=".$parentId)
->order($db->quoteName('lft') . ' ASC, '.$db->quoteName('id') . ' ASC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
foreach ($results as $key=>$val){
$results[$key]->children = $this->getChildren($val->id, $langTag);
}
return $results;
}
}
From what I can gather you have created a module and assigned it to specific pages. You haven't mentioned what the contents of the module are (custom html etc).
Have you assigned the module to the correct pages in the 'module assignment' tab? Have a look at this question and answer as it explains how to do that.
If you are seeing a white page, i'd suggest enabling error reporting in Joomla. This should provide you with additional useful information about the error.
If you have a link to your website that would be helpful, and the version of Joomla you are using.

OpenTok JWT Authenticacion Bug

When performing a REST request to the opentok rest API I was getting that my jwt token was "expired".
Wondering around a little bit, I performed a dummy request to the server
just for fetching the server date, by using the same date from the server as the token expiration time I was able to list videos belonging to a session.
This is clearly wrong, the iat time and the exp time should not match the server date.
Possible solutions:
A) The user should be able to specify his server time zone and the OpenTok REST server should match those dates regarding the time zone configured for a given project.
B) Disregard the iat and consider the expiration time in seconds.
Thanks
This is an indication that the clock on your server is not synced correctly. The PHP SDK from version 2.5.0 onwards has JWT implemented and has been proven to work correctly. I recommend you upgrade to v2.5.0 and ensure your server clock is accurate.
Patch
/**
* Useless class used to fix bugs and solve single session archive fetching
* issue in opentok.
*
* This class also implements JWT in order to comply with the new authentication
* system that will be in use during July of 2017.
*
* A problem was also detected when trying to authenticate (date issue)
*
* #see https://github.com/opentok/OpenTok-PHP-SDK/issues/172
* #see https://stackoverflow.com/questions/44768499/opentok-jwt-authenticacion-bug
*
* #author Federico Stange <jpfstange#gmail.com>
*/
namespace stange\opentok;
use \Firebase\JWT\JWT;
use \Guzzle\Common\Event;
use \OpenTok\Util\Client as OpenTokClient;
class OTAuthPlugin extends \OpenTok\Util\Plugin\PartnerAuth{
private $timestamp = null;
public static function getSubscribedEvents(){
return array('request.before_send' => 'onBeforeSend');
}
public function setTimestamp($time){
$this->timestamp =$time;
return $this;
}
public function getTimestamp(){
return $this->timestamp;
}
public function onBeforeSend(Event $event){
$event['request']->addHeader(
'X-OPENTOK-AUTH',
$this->createAuthHeader()
);
}
private function createAuthHeader(){
$token = array(
'ist' => 'project',
'iss' => $this->apiKey,
'iat' => $this->timestamp,
'exp' => $this->timestamp+180,
'jti' => uniqid()
);
return JWT::encode($token, $this->apiSecret);
}
}
class Client extends OpenTokClient{
public function configure($apiKey, $apiSecret, $apiUrl){
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->setBaseUrl($apiUrl);
$this->setUserAgent(OPENTOK_SDK_USER_AGENT, true);
$opentokAuthPlugin = new OTAuthPlugin($apiKey, $apiSecret);
$opentokAuthPlugin->setTimestamp($this->getServerDate());
$this->addSubscriber($opentokAuthPlugin);
$this->configured = true;
}
/**
* Make a request for getting the server date
* this is a bug and it has been reported to the opentok team.
* and to the tech support department.
*
*
*/
public function getServerDate(){
try{
$response = $this->get(
"/v2/project/". md5(uniqid())
)->send();
} catch (\Exception $e) {
$date = $e->getResponse()->getHeader('Date')->toArray();
$date = $date[0];
$serverDate = \DateTime::createFromFormat(
"D, d M Y H:i:s e",
$date
);
return $serverDate->getTimestamp();
}
return $serverDate;
}
public function listArchivesInSession($sessionId){
$url = "/v2/project/{$this->apiKey}/archive?sessionId=$sessionId";
$request = $this->get($url);
return $request->send()->json();
}
}

Resources