Skip to content

Instantly share code, notes, and snippets.

@taotiwordpress
Created August 24, 2020 17:43
Show Gist options
  • Save taotiwordpress/752bc33c38eeabc94585c99ba8a5b685 to your computer and use it in GitHub Desktop.
Save taotiwordpress/752bc33c38eeabc94585c99ba8a5b685 to your computer and use it in GitHub Desktop.
[apit insert posts] link to api and create posts in CPT #api #php
<?php
function insert_from_api() {
if(isset($_GET['input'])) {
// API URL
$url = '';
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'Name' => '',
'AuthenticationKey' => ''
);
//$payload = json_encode(array("user" => $data));
$payload= '{
"Request": {
"Name": "TaotiGetAllMerchandise",
"AuthenticationKey": "", "Parameters": { }
} }';
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$info = json_decode($result);
$terms = [];
foreach ($info->Records as $jsonItem) {
if(isset($jsonItem->NU__CategoryProductLinks__r->records)) {
foreach ($jsonItem->NU__CategoryProductLinks__r->records as $record) {
$terms[$record->NU__Category__r->NU__Title__c] = $record->NU__Category__r->NU__Title__c;
}
}
}
// var_dump($terms);
if(!empty($terms)) {
foreach ($terms as $term) {
wp_insert_term($term, "product-type");
}
}
foreach ($info->Records as $jsonItem) {
// var_dump($jsonItem->NU__Description__c);
// exit;
$terms = [];
if(isset($jsonItem->NU__CategoryProductLinks__r->records)) {
foreach ($jsonItem->NU__CategoryProductLinks__r->records as $record) {
$terms[$record->NU__Category__r->NU__Title__c] = $record->NU__Category__r->NU__Title__c;
}
}
if(!isset($jsonItem->Id)) {
continue;
}
$args = array(
'post_type' => 'products',
'meta_query' => array(
array (
'key' => 'product_id',
'value' => $jsonItem->Id,
'compare' => '=',
)
)
);
$query = new WP_Query($args);
// var_dump(isset($jsonItem->Format__c));
// exit;
$insert_args = array(
'post_title' => isset($jsonItem->Name)?$jsonItem->Name:NULL,
'post_type' => 'products',
'post_status' => 'publish',
'meta_input' => array(
'product_long_title' => isset($jsonItem->LongName__c)?$jsonItem->LongName__c:NULL,
'product_description' => isset($jsonItem->NU__Description__c)?$jsonItem->NU__Description__c:NULL,
'product_price' => isset($jsonItem->NU__ListPrice__c)?$jsonItem->NU__ListPrice__c:NULL,
'product_type' => isset($jsonItem->Format__c)?$jsonItem->Format__c:NULL,
'product_status' => isset($jsonItem->NU__Status__c)?$jsonItem->NU__Status__c:NULL,
'product_id' => isset($jsonItem->Id)?$jsonItem->Id:NULL,
'product_issue_date' => isset($jsonItem->IssueDate__c)?$jsonItem->IssueDate__c:NULL,
),
);
if(isset($query->posts[0])) {
// var_dump($query->posts[0]->ID);
$insert_args['ID'] =$query->posts[0]->ID;
}
$post_id = wp_insert_post($insert_args);
if(!empty($terms)) {
foreach($terms as $term) {
wp_set_object_terms( $post_id, $term, 'product-type' );
}
}
}
}
}
add_action( 'init', 'insert_from_api' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment