FAQPage schema in PHP not formatted correctly - json

I'm trying to build a schema for a page that has FAQ questions. The code works but something is wrong with the formatting. The problem is that the array "mainEntity" is on every question.
Here is my code
$posts = get_field('section_faq_relation');
if( $posts ):
$schema_faq_questions = array();
foreach( $posts as $post):
setup_postdata($post);
$headline = get_the_title();
$articlebody = get_the_content();
$schema_faq_question = array(
'#type' => 'Question',
'name' => $headline,
'acceptedAnswer' => array(
'#type' => 'Answer',
'text' => wp_filter_nohtml_kses( $articlebody )
)
);
array_push($schema_faq_questions, $schema_faq_question);
endforeach;
$schema_faq_all = array(
'#context' => 'http://schema.org',
'#type' => 'FAQPage',
'mainEntity' => $schema_faq_questions
);
echo '<script type="application/ld+json">' . json_encode($schema_faq_all) . '</script>';
endif;
wp_reset_postdata();
My code looks OK and returns this if I look at the json it's outputting. mainEntity is an array that holds all the questions.
{
"#context": "http://schema.org",
"#type": "FAQPage",
"mainEntity": [
{
"#type": "Question",
"name": "dolor sit amet",
"acceptedAnswer": {
"#type": "Answer",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing eli"
}
},
{
"#type": "Question",
"name": "quasi architecto",
"acceptedAnswer": {
"#type": "Answer",
"text": "re veritatis et quasi architecto beatae vitae dicta sunt explicabo."
}
}
]
}
But if I run the schema.org validator it returns:
Every question has "mainEntity". I don't know why it's doing that.

Related

how to create json response status in codeigniter

I made json code using json encode, the results are correct, but I want to add a satatus response to the object. How to ?
this my code
public function get(){
header('Content-Type: application/json');
$db = $this->M_order->db_order();
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['data'] = $data;
echo json_encode($response, TRUE);
}
this result my json
{
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
}
]
}
here I want to add a status header in object, like the following ...
{
"status": 200,
"error": false,
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
},
]
}
how to create ?
As I understand your question,
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['status'] = 200;
$response['error'] = false;
$response['data'] = $data;

How access array inside of JSON?

I'm use Guzzle for Laravel to send POST for Webservice, but I can't access array to send picture. That is the structed to I need follow:
{
"title": "xxxxxxxxxxxxxxxxx",
"category_id": "XXXX",
"official_store_id": null,
"pictures": [
{
"id": "XXXXX",
"url": "http://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"secure_url": "https://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"size": "500x500",
"max_size": "500x500",
"quality": ""
}
],
}
I try to send it like this:
$r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token='.$token->erp_access_token, [
'json' => [
'title' => $name,
'category_id' => $cat2,
'price' => $price,
'currency_id' => 'BRL',
'available_quantity' => $quantity,
'buying_mode' => 'buy_it_now',
'listing_type_id' => 'gold_special',
'condition' => 'new',
'description' => $description,
'pictures' => [
'url' => $image
]
]
]);
Returns the error:
{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was JSONObject value (truncated...)
I read the Guzzle docs, but I not found any example for that case.
Any suggestion?
Change your pictures value from this
'pictures' => [
'url' => $image
]
to this
'pictures' => [
['url' => $image]
]

how to save multiple column at one click in yii2

Sorry guys my title might not really explain what i want to achieve, i have an array of countries and i want to save it into the data base below is my code
public function actionCountries(){
$countries = array("AF" => "Afghanistan",
"AX" => "Ă…land Islands",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua and Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"YE" => "Yemen",
"ZM" => "Zambia",
"ZW" => "Zimbabwe");
$model = new Country();
foreach($countries as $code => $name){
$model->code = $code
$model->code = $name
if($model->save()){
echo $model->countryId;
}
}
}
i want to save all of the country into the database on one single click, but the result i got is empty, if i supply a false argument to the save() method i got the last element of the array saved into the database like below
$model = new Country();
foreach($countries as $code => $name){
$model->code = $code
$model->code = $name
if($model->save(false)){
echo $model->countryId;
}
}
this code will save only the last element in the array while the first code did nothing
Any help on this thanks
It's because
$model = new Country();
is outside of the loop so you are saving the same model over and over again. Move this inside the loop.
By the way - it's not efficient. Take a look at batchInsert() method for more memory-friendly way to do it.

Array push to multidimensional array for JSON

Can someone pls help me on how to array_push to a multidimensional array.
This is my code for pushing.
$arrDataChart3["categories"] = array();
array_push($arrDataChart3["categories"], array(
"category"=>array ()
)
);
while($row = sqlsrv_fetch_array($myAcc2)) {
array_push($arrDataChart3["categories"]["category"], array(
"label" => $row["position"])
);
}
i want to push $row["position"] to "category" from my database.
Here's what the array should contain.
"categories": [
{
"category": [
{
"label": "Q1"
},
{
"label": "Q2"
},
{
"label": "Q3"
},
{
"label": "Q4"
}
]
}
],
Thanks in advance
i used this solved the problem
$arrDataChart3["categories"] = array();
$arr=array ();
while($row = sqlsrv_fetch_array($myAcc2)) {
array_push($arr, array(
"label"=>$row['position']
)
);
}
array_push($arrDataChart3["categories"], array(
"category"=>$arr
)
);

How to suppress curl JSON response

Here is the code which in a file from the browser:
<?php
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://somesite/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "xxx",
"user" => "xxx",
"message" => "hello world",
)));
curl_exec($ch);
curl_close($ch);
?>
This runs properly and returns this to the browser:
{"status":1,"request":"34bb823184583c8bca6d9d56d297d795"}
I want to suppress that message and replace it with something else like a text string such as "worked". \
Displaying an image or pointing to a success .html page would be nice.
This is my first use of curl.
set CURLOPT_RETURNTRANSFER to true and let curl_exec($ch) return a value in a variable
<?php
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://somesite/messages.json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
"token" => "xxx",
"user" => "xxx",
"message" => "hello world",
)));
$result = curl_exec($ch);
curl_close($ch);
echo "some text"; // or echo file_get_content('some_html_page.html');
?>