Insert JSON into MongoDb directly from PHP - php

I have an Android application which sends json data to PHP script.
The PHP script have to save the data into MongoDB.
When I insert the data, MongoDB treat this data as a string.
$conn = new Mongo('mongodb://127.0.0.1:27017');
// access database
$db = $conn->Data;
// access collection
$collection = $db->Collection;
$collection->insert($myjson)
How can I say to MongoDB PHP driver that it is already a json document?
Thanks

The PHP MongDB Driver accepts arrays for inserts and queries (see here: http://www.php.net/manual/en/mongo.queries.php)
So you need to convert your JSON to an array.
Luckily, in general this is quite easy ... here is a snippet from a longer piece of code (see this article) to insert JSON data from the Twitter API into an array, then into MongoDB:
// Convert JSON to a PHP array
$usertimeline = json_decode($usertimeline);
// Loop array and create seperate documents for each tweet
foreach ($usertimeline as $id => $item) {
$collection->insert($item);
}
Note the json_decode() function can convert to an array by passing true as the second parameter.

Here's a tip for others who want to do this, but want to update records rather than insert new ones. In order to get Justin's approach to work, I had to make sure that I convert the _id object for each item to a MongoId:
// Convert JSON to a PHP array
$usertimeline = json_decode($usertimeline);
// Loop array and create seperate documents for each tweet
foreach ($usertimeline as $id => $item) {
$mongo_id = new MongoId($id);
$item->_id = $mongo_id;
$collection->update(array('_id' => $mongo_id), $item);
}

MongoDB uses BSON not JSON so you need to re-encode anyways. Also, I remember talking to kchodorow about this and the raw interface was not exposed then (been half a year at least) and I doubt it is now so even if you manage to get BSON data from somewhere , you can't avoid the de/encode.

As MongoDB PHP library supports conversion from JSON to BSON and BSON to PHP Std Class Object, You can follow simple steps.
//Convert Row (Document) to json
$json_row = json_encode($row);
//Convert JSON to BSON
$bson = \MongoDB\BSON\fromJSON($json_row);
//Convert BSON to PHP Std Class object
$row = \MongoDB\BSON\toPHP($bson);
// Insert Record to Document
$collection->insertOne($row);
You can get full MongoDB database backup and restore script from here
https://github.com/sulochanatutorials/php-mongodb-backup-restore

UPDATE 2021 WITH PHP 7 DRIVER:
Lets say your json string is
$myjson = {"name":"foo", "age":30}
First decode it with PHP json_decode. Which returns an array.
$myarraydata = json_decode($myjson)
Now you can directly insert it into mongoDB
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert($myarraydata);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $connection->executeBulkWrite('dbname.collection_name', $bulk, $writeConcern);
Check bulk operation official documentation here.

Related

How to get raw BSON from MongoDB with PHP "mongodb" extension

I am trying to get a binary BSON string from mongodb using PHP but I can't figure out how to get the document as BSON. It always parses it to a PHP object first.
I've tried to set a typeMap on the cursor but all this does is pass the already parsed PHP object to a bsonSerialize($object) function in the class.
The code looks like this:
<?php
$db = new \MongoDB\Driver\Manager('mongodb://localhost/test');
$query = new \MongoDB\Driver\Query([]);
$cursor = $db->executeQuery("test.contacts", $query);
foreach($cursor as $document) {
// $document is already parsed to a PHP stdObject
}
In recent MongoDB versions (since 3.6) the documents are returned as part of the responses, and the entire response is serialized as BSON. Meaning, the driver, when it receives a response, has to BSON-deserialize the entire response, and then part of the response will be given to the application as a found document.
There isn't a moment when the driver has just the document alone serialized as BSON.
So, if you need to have an individual document serialized as BSON, you'd need to do that yourself.

How to view a tinyblob object

I'm currently working with an existing database that is Mysql, and the system is built in php.
For whatever reason the builder of this system chose to store some parts of the data in blobs. One of them is a tiny blob.
In the database one of the records appears like this:
a:2:{i:0;s:3:"130";i:1;s:3:"182";}
This is viewable from the sql client I'm using. It says it's a TINYBLOB(255).
I need to be able to figure out the correct structure used to set this up so that I could build my part.
It appears to me as if I'm not seeing a "true" representation of what the data structure is.
I ran this on the php side:
public function types_get() {
$returnedTypes = $this->api->getReportTypes();
echo($returnedTypes);
$this->response($returnedTypes,REST_Controller::HTTP_OK);
}
It also produced this on the echo and response: a:2:{i:0;s:3:"130";i:1;s:3:"182";}
How would I be able to make it so I can see the true data as if it was a json string?
This data string has been created with the serialize() function. You can convert it back to a native array with the matching unserialize() function:
$string = 'a:2:{i:0;s:3:"130";i:1;s:3:"182";}';
$data = unserialize($string);
print_r($data);
Output:
Array(
[0] = 130
[1] = 182
)

How would I scrape this JSON info using PHP and MySQL?

Here's the info I'm trying to break up into a database. I'm going to be using this only for my own use to analyse statistics and all that. I have been manually doing it with Excel but I'd like to save myself some work in future.
URL IS: http://fantasy.premierleague.com/web/api/elements/537/
Any idea how to scrape that info or easily convert it to excel format? I know a bit of php and mysql, but nothing about JSON and very little about scraping (I tried messing with SIMPLE_HTML_DOM).
You need to JSON_decode the data in PHP.
$obj = JSON_decode($mydata));
print_r($obj);
Extra information for you:
http://php.net/manual/en/function.json-decode.php
You can convert it into an array as
$array = json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));
json_decode()
You could use PEAR excel writer to convert it into excel
PHP has a json parser function json_decode().
So:
Use the file_get_contents() function to read the json content from the URL into a string.
Use json_decode() to create a PHP structure representation.
Use PEAR Spreadsheet_Excel_Writer module to create your excel spreadsheet.
Yeah. Easy as 1, 2, 3.
<?php
$x=json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));
print_r($x);//$x will contain all the values in an array format.
?>
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$json= file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/', false, $context);
$arr= json_decode($json);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($arr, array ($xml, 'addChild'));
print $xml->asXML();
PHP makes it very easy:
$str = file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/');
$jsonarray = json_decode($str, true);
var_dump($jsonarray);
Of course you will have to analyze the structure of the array and figure out how to decompose it to what you are actually looking for.
Try using $obj = json_decode($jsonStr) after you receive your response string by running curl on the URL you mentioned in PHP. Then you can grab params from the json object like
$obj['paramName'];
Then you can do anything you want with the information including putting it into a database.
For simple MySQL interaction in php, check out MySQLConnector class.
http://jakesankey.com/blog/2011/12/php-mysql-helper-class/
use just json_decode and get the converted data like this edit
$arr = json_decode('your JSON data',true);
echo $arr['transfers_out']; // output 490374 //for array
echo $arr->transfers_out; // output 490374 //for stdClass

PHP build a specific JSON string from postgresql Database

I want use zachhunters json to html tabel script with my postressql db so i need a specific JSON string format but i dont know how to build it ... (i am new in json)
Link to the Script
I get my data using
$result = pg_fetch_array($rs, NULL, PGSQL_ASSOC);
When i use json_encode($result) i get this:
{"id":"2","surname":"Max","name":"Muster"} etc.
For the Script i need somthing like:
{ "d": "[{\"id\":1,\"Username\":\"Sam Smith\"},{\"id\":2,\"UserName\":\"Fred Frankly\"}]" }
How can i handle this?
In each loop where you fetch an element from your database store the $result in an array $rows.
After the loop simply use json_encode(array('d' => $rows)) to create JSON containing all your data.

How to build JSON array before posting to other document?

I am reading one document requests processed trough another document using AJAX.
In document processed trough AJAX i want to generate JSON array because its only way i can pass two variables and then spread them like this
$('#country').append($('<option>').text(arr_values[1]).attr('value', arr_values[0])));
now i generating code like this
$results2 = mysql_query('SELECT full, short FROM `Countries` WHERE '.$cities);
$json = array();
while( $result2 = mysql_fetch_array($results2) ) {
$json[] = $result2['short'].','.$result2['full'];
}
json_encode manual is pretty clear about its usage, check 'Example #2 A json_encode() example showing all the options in action' in http://ar.php.net/manual/en/function.json-encode.php
json_encode converts standard php arrays to JSON.
Anyway, your SQL code is not correct. You must use mysql_fetch_array for getting the values from results2, and 'short' and 'full' are not in your query.

Categories