2

I have a json encoded data,which I need to upload to aws dynamoDB.
I followed these link1 & link2 links. but my datas are not uploading to the DB.
I am new to dynamo.Can anyone help me..
let me share my json..

Json.php

header('Content-Type: application/json');

$feed_url = 'http://localhost/json/j_xml.xml'; 
$xml_data = simplexml_load_file($feed_url);

// -------------------------------------------------------------------- 

$i=0; 
$response   =   array('return' => true,'message' => 'success','details' => array() );
foreach($xml_data->channel->item as $ritem) { 
$e_wp                           = $ritem->children("wp", true);
$e_author                       = $ritem->children("dc", true);
$e_content                      = $ritem->children("content", true);
// --------------------------------------  
if((string)$e_wp->status =='publish') {
$post_id                        = (string)$ritem->guid; 
$post_id                        = explode('=', $post_id);
$content['article_post_id']     = $post_id[1]; 
$content['article_title']       = (string)$ritem->title; 
$content['article_cat_slug']    = 'News'.$post_id[1]; 
$content['article_mob_title']   = (string)$ritem->title; 
$content['article_category']    = (string)$ritem->category; 
$content['article_pub_date']    = (string)$e_wp->post_date;
$content['article_description'] = (string)$ritem->description; 
$content['article_content']     = (string)$e_content->encoded; 
$content['article_author']      = (string)$e_author->creator; 
$content['article_seo_desc']    = ''; 
$content['article_seo_tags']    = ''; 
$content['article_fb_title']    = ''; 
$content['article_fb_desc']     = ''; 
$content['article_twitter']     = '';
$content['article_create_date'] = (string)$e_wp->post_date_gmt;
$content['article_status']      = (string)$e_wp->status;
}
array_push($response['details'],$content);
} 
echo json_encode($response,JSON_PRETTY_PRINT);   

1 Answer 1

1

I have figured out the solution.
I think this may help some one in future.
I followed the Marshaler method.

require('aws/aws-autoloader.php');
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;

// --------------------------------------------------------------------

$feed_url = 'http://localhost/json/j_xml.xml'; 
$xml_data = simplexml_load_file($feed_url);

// -------------------------------------------------------------------- 

$i=0; 
$results    =   array('return' => true,'message' => 'success','details' => array(),'response'=>array());
$AWS_ACCESS_KEY_ID  = '******************';
$AWS_SECRET_ACCESS_KEY = '*******************';
$AWS_REGION = '<region-code>';

$client = DynamoDbClient::factory(array(
            'version'     => '2012-08-10',
            'credentials' => array(
            'key'    =>  $AWS_ACCESS_KEY_ID,
            'secret' => $AWS_SECRET_ACCESS_KEY,
            ),
            'region' => $AWS_REGION
            ));

$marshaler = new Marshaler();
foreach($xml_data->channel->item as $ritem) { 
$e_wp                           = $ritem->children("wp", true);
$e_author                       = $ritem->children("dc", true);
$e_content                      = $ritem->children("content", true);
// --------------------------------------  
if((string)$e_wp->status =='publish') {
$post_id                        = (string)$ritem->guid; 
$post_id                        = explode('=', $post_id);
$content['ArticleID']           = $post_id[1]; 
$content['article_title']       = (string)$ritem->title; 
$content['article_cat_slug']    = 'News'.$post_id[1]; 
$content['article_mob_title']   = (string)$ritem->title; 
$content['article_category']    = (string)$ritem->category;
$content['article_pub_date']    = (string)$e_wp->post_date;
$content['article_description'] = (string)$ritem->description; 
$content['article_content']     = (string)$e_content->encoded; 
$content['article_author']      = (string)$e_author->creator; 
/* DynamoDB doesnot accept empty string
$content['article_seo_desc']    = ''; 
$content['article_seo_tags']    = ''; 
$content['article_fb_title']    = ''; 
$content['article_fb_desc']     = ''; 
$content['article_twitter']     = '';
*/
$content['article_create_date'] = (string)$e_wp->post_date_gmt;
$content['article_status']      = (string)$e_wp->status; 
$content = array_filter($content);
$json =  json_encode($content,JSON_PRETTY_PRINT);
$response = $client->putItem([
    'TableName' => 'Article',
    'Item'      => $marshaler->marshalJson($json)
]);

}
array_push($results['details'],$content);
array_push($results['response'],$response);

} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.