Currently Im trying to use elliots twitter library for CI at http://www.haughin.com/code/twitter/ after installation, it went well. the source code worked well..
Then I try to add code at index() function which is like this :
function index()
{
echo 'hi there';
$user = $this->tweet->call('get', 'account/verify_credentials');
$dec = json_decode($user);
}
I tried to decode json by using json_decode() function but it return error
json_decode() expects parameter 1 to be string, object given
This is my first time working with json..
Is there something I missed out ?
Thank you..
You should juggling that object into string type...
$user = (string) $this->tweet->call('get', 'account/verify_credentials');
$dec = json_decode($user);
It doesn`t need any variable convertion.
just access object directly like $dec->username
Related
Current PHP version: 7.1.7
please help me I have this problem
Illegal offset type in isset or empty at line 113
I have this problem after I decided to upload files to my website
in C:\xampp\htdocs\archive\vendor\yiisoft\yii2\web\UploadedFile.php
line 113:
public static function getInstanceByName($name)
{
$files = self::loadFiles();
return isset($files[$name]) ? new static($files[$name]) : null;
}
and guys if this not clear ask me for more explication and thanks
This code in models
[['file'],'file'],
this code in controllers
$docfileload = $model->docname;
$model->file = UploadedFile::getinstancebyname($model,'file');
$model->file->saveAs('uploads/'.$docfileload.'.'.$model->file->extension);
$model->save();
//save path in db
$model->docfile = 'uploads/'.$docfileload.'.'.$model->file->extension;
this code in _form
<?= $form->field($model,'file')->fileinput(); ?>
in first I used getInstance() but replecd getInstanceByName() because had porblem here
error yii2 upload Call to a member function saveAs() on null
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
You function is defined as getInstanceByName($name), but you are calling it using getinstancebyname($model,'file')
Something wrong there
in first I used
$model->file = UploadedFile::getinstancebyname($model,'file');
but right is to use
$model->file = UploadedFile::getinstance($model,'file');
I have a problem with my router in Phalcon.
I have an action in my controller which ether takes a date parameter or not.
So when I access an URL: http://example.com/sl/slots/index/2017-06-27
everything works ok.
But when I go to: http://example.com/sl/slots/index
I get the following error:
DateTime::__construct(): Failed to parse time string (sl) at position
0 (s): The timezone could not be found in the database.
So the router actually takes the "sl" in the beginning as a parameter.
My router for this kind of url is set like this:
$router->add(
"/{language:[a-z]{2}}/:controller/:action",
array(
"controller" => 2,
"action" => 3
)
);
Btw it does the same withut the index: http://example.com/sl/slots
Oh and my slots index action looks like this:
public function indexAction($currentDate = false){ //code }
So the $currentDate is set to "sl" when I call the action without a parameter
Thank you for the help
Well you need to add language in first argument of action too. Then it should work.
In addition to #Juri's answer.. I prefer to keep my Actions empty or as slim as possible. Imagine if you have 3-4 parameters in the Route, you will end up with something like:
public function indexAction($param1 = false, $param2 = false, $param3 = false....)
Here is how I prefer to handle Route parameters:
public function indexAction()
{
// All parameters
print_r($this->dispatcher->getParams());
// Accessing specific Named parameters
$this->dispatcher->getParam('id');
$this->dispatcher->getParam('language');
// Accessing specific Non-named parameters
$this->dispatcher->getParam(0);
$this->dispatcher->getParam(1);
...
}
How to parse the Url which in the format as shown
http://localhost/CorrectCodeToDeploy/healthkatta/index.php?r=site%2FArticle%2F2013%2F07%2F13%2FDisadvantages-of-smoking-51e12690a5a58
Want to get rid of the special characters %2Fand put / instead . I have shared a link of an article to facebook
but when i access this link from facebook it comes in the format as mentioned above and the content is not properly accessed in Yii.How can i write the pattern to access this kind of url in yii
Maybe you can directly decode your $_GET in your index.php file, something like:
$_GET = array_map('urldecode', $_GET);
Please be aware the above doesn't work with nested arrays, so you need to create a more in depth recursive function like (not tested though) :
function urldecodeArray($strArr){
if (!is_string($strArr) && !is_array($strArr)) {
return $strArr;
}
if(is_string($strArr)) {
return urldecode($strArr);
}
foreach ($strArr as $key=>$value) {
$strArr[$key] = urldecodeArray($value);
}
return $strArr;
}
$_GET = urldecodeArray($_GET);
You can just to use urldecode() function
I am trying to use following construct
#ApplicationPath("app")
#Path("api/{userid}/model")
public class ModelService
{
#Get
#Path("{modelid: (.*)?}")
public Response removePreProcessor(#PathParam("userid") String sUserId, #PathParam("preprocessorid") String sPreProcessorId)
{
return Response.build();
}
}
I can not access both following REST URL
GET http://localhost:8080/XXXX/app/api/xyz/model
GET http://localhost:8080/XXXX/app/api/xyz/model/123
Let me know what is a wrong I am doing
-Thanks in advance
As I read your question, several things look strange to me but they may be context related.
One thing though seems wrong :
You are using a #PathParam("preprocessorid") but I can't see this param in your path.
Do you have any logs ?
Is it possible to define a method like below to receive $id?
public function action_delete($id){
}
I've defined a route
Route::set('tweet', 'tweet/delete/<id>', array('id' => '\d+'))->defaults(array(
'controller'=>'tweet',
'action'=>'delete'
));
I remember a code snippet something like this few months ago...
Updated: I get the following error message
Missing argument 1 for Controller_Tweet::action_delete()
If your Kohana version < 3.2 then you can use this, however it is highly recommended that you get the id value with $this->request->param('id') -> this is the only way since 3.2 version:
public function action_delete(){
$id = $this->request->param('id');
// Rest of tour code
}
In Kohana 3.2, this doesn't work anymore. You'll have to retrieve the variable through
$id = $this->request->param('id');
See: http://kohanaframework.org/3.2/guide/api/Request#param