cURL retrieve json data and store in mysql database from url - mysql

i'm facing problems into cURL. here i can get the json data from another source code and also i got some ideas. but really i can't save the json data into my own server in sql database. i wanna to retrieve the data from
url: https://jamuna.tv/wp-json/wp/v2/posts
and wanna to save into my server (mysql).
here is the $url json data i wanna to save in mysql server:
id
date
link
title
content
author
categories
wp:attachment
and i want to save them into mysql database. my table name is "news" and i want to save them into my table columns.
id [id]
title [title]
description [content]
date [date]
category [categories]
thumbnail [wp:attachment]
admin [author]
here i'm mark the json from url and replaced into my sql columns name. if anyone can give me the instructions about how i can fetch the data from user and save into mysql.
thanks advance.

There is an implementation of your desired with PHP. You must check if values are valid or not or if the array members exist or not. This is just a primitive implementation of your example.
<!DOCTYPE html>
<html>
<body>
<?php
// db connection
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db = new PDO('mysql:host=localhost;dbname=test;', 'user', 'pass', $options);
// download json from url
$json = file_get_contents('https://jamuna.tv/wp-json/wp/v2/posts');
echo '<br/>';
$sql = "INSERT INTO `mytable` (`id`,`title`,`description`,`date`,`category`,`thumbnail`,`admin`)
VALUES (:id, :title, :content , :date, :categories, :attachment, :author)";
$stm = $db->prepare($sql);
// parse JSON
$arr = json_decode($json, true);
$i = 0;
foreach ($arr as $record) {
echo "==================Insert record " . $i ++ . "<br>";
$data = array(
':id' => $record['id'],
':title' => $record['title']['rendered'],
':content' => $record['content']['rendered'],
':date' => $record['date'],
':categories' => $record['categories'][0],
':attachment' => $record['_links']['wp:attachment'][0]['href'],
':author' => $record['author']
);
var_dump($data);
// inserting a record
$stm->execute($data);
}
?>
</body>
</html>

Related

How to upload CSV file into more than one database table using MySql?

I want to upload a .csv file into a database but in 3 different table. I mean like in the .csv file have three columns then I want to insert each column into different table. How can I do it? Can anyone give any example or idea to do this.
Here is an example if you wish to upload csv file in db
public function upload(Request $request){ //upload csv
$file = $request->file('file');
$csvData = file_get_contents($file);
$rows = array_map("str_getcsv", explode("\n", $csvData));
//CSV headers
$header = array_shift($rows);
$escapedHeader=[];
//to converting lowercase and remove spaces
foreach ($header as $key => $value) {
$lheader=strtolower($value);
$escapedItem=preg_replace('/[^a-z]/', '', $lheader);
array_push($escapedHeader, $escapedItem);
}
//storing data to database
foreach($rows as $row) {
if (count($header) != count($row)) {
continue;
}
//This will generate a associate array with headers.
$row = array_combine($escapedHeader, $row);//dd($row);
// if there are 3 tables named - student, course, grade
Student::create([
'fname' => $row['firstname'],
'lname' => $row['lastname'],
]);
Course::create([
'course' => $row['course'],
]);
Grade::create([
'grade' => $row['score'],
]);
}
Session::flash('message', 'CSV file imported!');
return response()->json('success');
}

Unable to open file for reading [filename.pdf] yii2 swiftmailer

I am trying to send an email with attachment when I used var_dump($filename)it returns the filename and gettype($filename) it returns string. but when I am trying to send an attachment it still returns Unable to open file for reading [filename.pdf] even if $file_attachment was looped I tried to change UploadedFile::getInstancesByName('file_attachment'); to UploadedFile::getInstanceByName('file_attachment'); but nothing happened. Please help me.
This is my controller
if(Yii::$app->request->isPost){
$email = Yii::$app->request->post('email');
$message = Yii::$app->request->post('message');
$file_attachment = UploadedFile::getInstancesByName('file_attachment');
if($file_attachment){
$mail = Yii::$app->mailer->compose()
->setFrom(['myemail#gmail.com' => 'My Email'])
->setTo($email)
->setSubject('My Subject')
->setHtmlBody($message);
foreach ($file_attachment as $file) {
$filename = $file->baseName. '.' . $file->extension;
$mail->attach($filename);
}
//$mail->send();
//echo gettype($filename);
// var_dump($filename);
$mail->send();
}else{
$mail = Yii::$app->mailer->compose()
->setFrom(['myemail#gmail.com' => 'My Email'])
->setTo($email)
->setSubject('My Subject')
->setHtmlBody($message)
->send();
}
}
This is the view
<?php
echo FileInput::widget([
'name' => 'file_attachment',
'attribute' => 'file_attachment',
'options' => ['multiple' => true]
]);
?>
The baseName property of yii\web\UploadedFile contains the original filename but the file is not present on the server under its original filename. You need to use tempName property that contains path to the uploaded file on server.
Your for each cycle that attaches files to mail should look like:
foreach ($file_attachment as $file) {
$filename = $file->baseName. '.' . $file->extension;
$mail->attach(
$file->tempName,
['fileName' => $filename]
);
}
It might also be a good idea to check hasError property of yii\web\UploadedFile before attempting to attach file to see if the upload was successful.
Also make sure that you've set 'enctype' => 'multipart/form-data' in you form options when you are not using ActiveForm with ActiveField::fileInput() otherwise the files might not be uploaded.

How to output geoJSON file from mysql spatial table?

I have a mysql spatial database ('mydb1') of one table ('pk'), that i obtained from a point shapefile using ogr2ogr tool. one row of this 'pk' table, is like:
OGR_FID SHAPE CODIF Position_X Position_Y Nom Status
1 [GEOMETRY-25o] PAC182854 398235.38 414569.24 G-31 Vert
I need to output the geoJSON file of this table. I downloaded geoPHP library and used MySQL to GeoJSON script which i adapted to my settings, like below:
<?php
/**
* Title: MySQL to GeoJSON (Requires https://github.com/phayes/geoPHP)
* Notes: Query a MySQL table or view and return the results in GeoJSON format, suitable for use in OpenLayers, Leaflet, etc.
* Author: Bryan R. McBride, GISP
* Contact: bryanmcbride.com
* GitHub: https://github.com/bmcbride/PHP-Database-GeoJSON
*/
# Include required geoPHP library and define wkb_to_json function
include_once('geoPHP/geoPHP.inc');
function wkb_to_json($wkb) {
$geom = geoPHP::load($wkb,'wkb');
return $geom->out('json');
}
# Connect to MySQL database
$conn = new PDO('mysql:host=localhost;dbname=mydb1','root','admin');
# Build SQL SELECT statement and return the geometry as a WKB element
$sql = 'SELECT *, AsWKB(SHAPE) AS wkb FROM pk';
# Try query or error
$rs = $conn->query($sql);
if (!$rs) {
echo 'An SQL error occured.\n';
exit;
}
# Build GeoJSON feature collection array
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
# Loop through rows to build feature arrays
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
$properties = $row;
# Remove wkb and geometry fields from properties
unset($properties['wkb']);
unset($properties['SHAPE']);
$feature = array(
'type' => 'Feature',
'geometry' => json_decode(wkb_to_json($row['wkb'])),
'properties' => $properties
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}
header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
$conn = NULL;
?>
But when i execute the code on the browser, i get totally a blank page. What's wrong should i fix to output my geoJSON file plz?
You should check the php logs for more information in case an error ocured. Make sure error reporting and display are enabled:
error_reporting(E_ALL);
ini_set('display_errors', 1);
if no error show up inspect the $geojson object easiest would be using
print_r($geojson);

Wordpress pods: Exporting specific columns to json

I have the following code for exporting all the items in one of my pods to json. The thing is I don't need all the 130 columns in the json file, but only about 20. Since this will be done for about 150 items I thought I could save some loading time by not printing out all the fields, but I do not know how to do this. For example I only want to print the column value named 'title' for all items in the pod. My code is attached bellow.
<?php
$pods = pods('name', array('orderby' => 'name asc', 'limit' => -1));
$all_companies = $pods->export_data();
if ( !empty( $all_companies ) ) {
die(json_encode($all_companies);
}else{
die(json_encode(array('error' => 'No cars found.')));
}
?>
I thought about doing something like this:
if ( 0 < $all_companies->total() ) {
while ($all_companies->fetch()) {
$json .= $all_companies->field('title');
}
$json = rtrim($json, ",");
$json .= '}}';
}
echo $json;
But it doesn't work and also the code becomes very long.
I'd make an array of the names of the twenty fields you want then build an array of those fields for each item, by doing a foreach of those field names passed to Pods::field() inside the while loop. Like this:
$pods = pods('name', array('orderby' => 'name asc', 'limit' => -1));
$fields = array( 'field_1', 'field_2' );
if ( $pods->total() > 0 ) {
while ( $pods->fetch() ) {
foreach ( $fields as $field ) {
$json[ $pods->id() ] = $pods->field( $field );
}
}
$json = json_encode( $json );
}
Alternatively, you could hack the /pods/<pod> endpoint of our JSON API to accept a list of fields to return as the body of the request. Wouldn't be hard to do, make sure to submit a pull request if you make it work.

Migrate Old Site Users into Magento DB

I have more than 300,000 users on an old online store. Client switched to Magento solution and now have to migrate all the users, addresses to Magento. So I have to write a custom script to import users and their address to the Magento system.
Are there any tutorials or similar sort of work already done. Please help me.
Thanks
Here's an example of how I migrated users from OSC into Magento with the SOAP library. This script was run on the old server and needs to be run from the ssh command line (php execution time through the browser will not support this
$proxy = new SoapClient('http://[your magento url]/api/soap/?wsdl=1');
$sessionId = $proxy->login('admin', '[your password]');
// connect to local db
$link = mysql_connect('localhost', '[old ecommerce db]', '[old db pw]');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('sbc_osc', $link);
$sql = "SELECT * FROM customers";
$customers = mysql_query($sql);
// loop thyrough customers
while ($customer = mysql_fetch_assoc($customers)) {
set_time_limit(600);
$newCustomer = array(
'firstname' => $customer['customers_firstname'],
'lastname' => $customer['customers_lastname'],
'email' => $customer['customers_email_address'],
'password_hash' => $customer['customers_password'],
'store_id' => 2, // set the store you want to send to
'website_id' => 2
);
$telephone = $customer['customers_telephone'];
$fax = $customer['customers_fax'];
try{
$newCustomerId = $proxy->call($sessionId, 'customer.create', array($newCustomer));
}
catch (Exception $e) {
echo "failed to create customer for: " . $customer['customers_firstname'] . " " . $customer['customers_lastname'] . "\n";
}
// grab the default address
$sql = "SELECT ab.*, c.countries_iso_code_2, z.zone_name, z.zone_id
FROM address_book ab
LEFT JOIN countries c ON ab.entry_country_id = c.countries_id
LEFT JOIN zones z ON ab.entry_zone_id = z.zone_id
WHERE customers_id = {$customer['customers_id']} AND address_book_id = {$customer['customers_default_address_id']}";
$addresses = mysql_query($sql);
while ($address = mysql_fetch_assoc($addresses)) {
$newCustomerAddress = array(
'firstname' => $address['entry_firstname'],
'lastname' => $address['entry_lastname'],
'company' => $address['entry_company'],
'country_id' => $address['countries_iso_code_2'],
'region_id' => $address['zone_id'],
'region' => ($address['zone_name'] != "" ? $address['zone_name'] : $address['entry_state']),
'city' => $address['entry_city'],
'street' => array($address['entry_street_address']),
'telephone' => $telephone,
'fax' => $fax,
'postcode' => $address['entry_postcode'],
'is_default_billing' => true,
'is_default_shipping' => true,
);
try{
$newAddressId = $proxy->call($sessionId, 'customer_address.create', array($newCustomerId, $newCustomerAddress));
}
catch (Exception $e) {
echo "failed to add address for: " . $address['entry_firstname'] . " " . $address['entry_lastname'] . "\n";
}
}
echo "migrated: " . $customer['customers_firstname'] . " " . $customer['customers_lastname'] . "\n";
}
mysql_close($link);
One thing you need to watch out for is the passwords.. for this to work I had to set up Magento to use the same password hashing schema.
My suggestion would be to look into the customer import api and build out a script using the methods from the code base, using the API will be slower so since you will be running the script on your server you can build it using the actual methods. So you can look at this folder for the customer api classes and methods
/app/code/core/Mage/Customer/Model/Customer
and then here for the address api classes and methods
/app/code/core/Mage/Customer/Model/Address/
You would probably need to export your data to CSV and get it in the right format to import into Magento. 30k isn't that much so you can even try out the normal import process that is default Magento. We haven't had good luck with that but we have been importing hundreds of thousands of customers. Even then we break the file down into small chunks of customers at a time.