Drupal create node by post Api call fail with message "Could not determine entity type bundle: \\u0022type\\u0022 field is missing."} - api

I'm trying to create a node via drupal API but I have this error:
Got error 'PHP message: PHP Fatal error: Uncaught GuzzleHttp\\Exception\\ClientException: Client error: `POST https://site.it/entity/node?_format=hal_json` resulted in a `422 Unprocessable Entity` response:\n{"message":"Could not determine entity type bundle: \\u0022type\\u0022 field is missing."}
this is my function:
public function createFaq($notes, $telegram_id){
$url = "/entity/node?_format=hal_json";
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
[
'type' => [ ['target_id' => 'faq'] ],
'title' => 'title',
'utente' => [ [ 'target_id' => '123462' ] ],
'field_domanda' => [ [ 'value' => $notes['domanda'] ] ],
'field_presenza' => [ [ 'value' => $notes['presenza'] == "Si"? true : false ] ],
]
])
];
$response = $this->client->request('POST', $url , $opt);
$r = json_decode( $response->getBody());
return $r;
}
But i't really strange because this other function is working
public static function createUser($title){
$url= "/entity/node?_format=hal_json";
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
'title' => [ [ 'value' => $title ] ],
'type' => [ [ 'target_id' => 'article' ] ],
])
];
$response = $this->client->request('POST', $url , $opt);
$r = json_decode( $response->getBody());
return $r;
}
Can someone understood my error?

This is because the json data are enclosed in square brackets twice, just remove one pair :
$opt = [
'headers' => self::$baseHeader,
'body' => json_encode([
//[
'type' => [ ['target_id' => 'faq'] ],
'title' => 'title',
'utente' => [ [ 'target_id' => '123462' ] ],
'field_domanda' => [ [ 'value' => $notes['domanda'] ] ],
'field_presenza' => [ [ 'value' => $notes['presenza'] == "Si"? true : false ] ],
//]
])
];

Related

Convert Array Into String Array

array:5 [
0 => array:1 [
"location_id" => 1
]
1 => array:1 [
"location_id" => 4
]
2 => array:1 [
"location_id" => 6
]
3 => array:1 [
"location_id" => 7
]
4 => array:1 [
"location_id" => 8
]
]
convert this into ["1","4","6","7","8",]
as used this ["1","4","6","7","8",]array in different query
You can use Laravel Collection pluck method to only return property which you want from each array item, and after that flatten the result array with flatten
$data = [
[
"location_id" => 1
],
[
"location_id" => 4
],
[
"location_id" => 6
],
[
"location_id" => 7
],
[
"location_id" => 8
]
];
$result = collect($data)->pluck('location_id')->flatten();
You can use the laravel helper array flatten method: Read more about it from here: https://laravel.com/docs/9.x/helpers#method-array-flatten
// Add the helper class call in the controller header
use Illuminate\Support\Arr;
// The actual array
$array = [
0 => [
"location_id" => 1
],
1 => [
"location_id" => 4
],
2 => [
"location_id" => 6
],
3 => [
"location_id" => 7
],
4 => [
"location_id" => 8
]
];
// Flatten the array function
$result = Arr::flatten($array);
Results:
['1','4','6','7','8']
Not as clean you might want but get the job done:
$resultSet = collect($data)->map(function($item){
return $item['location_id'];
})->toArray();
$resultString = "[";
foreach($resultSet as $item){
$resultString .= "'{$item}'" . ",";
}
$resultString = rtrim($resultString, ","); // produces this: "['1','4','6','7','8']"
$resultString .= "]";
dd($resultString);
You can use the laravel helper array pluck method Read more about it from here: https://laravel.com/docs/9.x/helpers#method-array-pluck
$array = [
0 => [
"location_id" => 1
],
1 => [
"location_id" => 4
],
2 => [
"location_id" => 6
],
3 => [
"location_id" => 7
],
4 => [
"location_id" => 8
]
];
$data = \Arr::pluck($array, 'location_id'); // [1,4,6,7,8]
$result = array_map('strrev', $data);
Result
["1","4","6","7","8"]

How to Alias one of the table sql in cakephp 3.6.3

I'm trying to access Contacts. I'm getting below error
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Contacts'
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
Check SQL Query Screenshot
How do I setAlias() in table associations ?
ContactsController.php
public function index()
{
$this->paginate = [
'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status',
'Secteurs', 'Products']
];
$contacts = $this->paginate($this->Contacts);
$this->set(compact('contacts'));
}
public function view($id = null)
{
$contact = $this->Contacts->get($id, [
'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status',
'Secteurs', 'Products', 'Leads', 'Accounts']
]);
$this->set('contact', $contact);
}
ContactsTable.php
$this->setTable('contacts');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id'
]);
$this->belongsTo('Contacts', [
'foreignKey' => 'contact_type_id'
]);
$this->belongsTo('Leads', [
'foreignKey' => 'lead_id'
]);
$this->belongsTo('SourceProspects', [
'foreignKey' => 'source_prospect_id'
]);
$this->belongsTo('Accounts', [
'foreignKey' => 'account_id'
]);
$this->belongsTo('Status', [
'foreignKey' => 'statut_id'
]);
$this->belongsTo('Secteurs', [
'foreignKey' => 'secteur_id'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->hasMany('Accounts', [
'foreignKey' => 'contact_id'
]);
$this->hasMany('Leads', [
'foreignKey' => 'contact_id'
]);
}
The problem seems to be in ContractsTable here
$this->belongsTo('Contacts', [
'foreignKey' => 'contact_type_id'
]);
in this way cake join the Contacts table with itself so creating a non unique alias
maybe is just a typo and you wanted to do
$this->belongsTo('ContactTypes', [
'foreignKey' => 'contact_type_id'
]);
but if you actually want to use that relationship then you have to alias the joined Contacts table
$this->belongsTo('ParentContacts', [ // choose your alias here
'className' => 'Contacts'
'foreignKey' => 'contact_type_id'
]);
so every time you have to refer to the joined table you can do something like
'contain' => [
...,
'ParentContacts',
],

How should I send file to api server in laravel by Guzzle Client

Could you please tell me how should I send a file to upload on api?
In api server we have a post method which it gives two parameters:
one name in query and a file in formData then it gives a link as response body.
I'm going to send file via guzzle client in this format:
$file = $request->file('InputFile');
$file_path = $file->getPathname();
$response = $this->CX_Client->post('/file/upload?name='.$fileName, [
'formData' =>
[
'file' =>
[
'name' => 'InputFile',
'contents' => fopen($file_path, 'r'),
'filename' => $fileName
]
]
]);
But this does not work and server can not find its parameters in this request. What's wrong with me?
You should use multipart instead of formData
https://guzzle.readthedocs.io/en/latest/request-options.html#multipart
$response = $this->CX_Client->post('/file/upload?name='.$fileName, [
'multipart' => [
[
'name' => 'foo',
'contents' => 'data',
'headers' => ['X-Baz' => 'bar']
],
[
'name' => 'baz',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'qux',
'contents' => fopen('/path/to/file', 'r'),
'filename' => 'custom_filename.txt'
],
]
]);

yii2:Call to undefined method Closure::allows() in denyCallback

i use Yii2 framework
this is my behaviors function in ShippingController
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
// deny all POST requests
// [
// 'actions' => ['index', 'create'],
// 'allow' => TRUE,
// ],
[
'actions' => ['index', 'create', 'init'],
'allow' => true,
'roles' => ['?'],
'matchCallback' => function ($rule, $action)
{
return $action->controller->redirect('action');
}
],
'denyCallback' => function ($rule, $action)
{
return $action->controller->redirect('not');
}
// everything else is denied
],
],
];
}
matchCallback is worked good but
indenyCallback i have this error :
Error
Call to undefined method Closure::allows()
what is my problem ???!
move denyCallback element outside of the rules array
it needs to be on the same level with class and rules

Correct JSON ouput iPhone json_encode

I'm trying to get a correct json output for iPhone i'm using the code:
<?php
$post = array('items' => 1, 'title' => message, 'description' => description);
echo json_encode($post);
?>
And the output is:
{"items":1,"title":"message","description":"description"}
But i want a output with [
{
"items": [
{
"title": "message",
"description": "description"
}
]
}
Can someone tell me how to do that?
try
$post = array('items' => array( 0 => array('title' => message, 'description' => description)));
(warning: untested!)