Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function addPost( $post ) {
- $image = null;
- if ($post->picture) {
- $image = extractFeaturedImage( $post->picture );
- }
- if ($post->news_photo) {
- $image = extractFeaturedImage( $post->news_photo );
- }
- $post->{'featured_media'} = null;
- if ( $image ) {
- $result = invokeApi( getFullUrl( 'media' ), $image['file'], [
- 'cache-control: no-cache',
- "content-disposition: attachment; filename={$image['name']}",
- 'content-type: image/jpg'
- ] );
- invokeApi( getFullUrl( "media/{$result->id}" ), [ 'description' => $post->tags ], [ 'Content-type: application/json' ], 1 );
- $post->featured_media = $result->id;
- }
- invokeApi( getFullUrl( 'posts' ), extractPost( $post ), [ 'Content-type: application/json' ], 1 );
- }
- function invokeApi( $url, $data, $headers, $json = 0 ) {
- $ch = curl_init();
- $username = 'admin';
- $password = '1234qwer';
- $mainHeader = [ 'Authorization: Basic ' . base64_encode( $username . ':' . $password ) ];
- $combined = array_merge( $headers, $mainHeader );
- if ( $json ) {
- $data = json_encode( $data );
- }
- curl_setopt( $ch, CURLOPT_URL, $url );
- curl_setopt( $ch, CURLOPT_POST, 1 );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_HTTPHEADER, $combined );
- $result = curl_exec( $ch );
- curl_close( $ch );
- return json_decode( $result );
- }
- function getFullUrl( $uri ) {
- return "http://newsmaker-wp.dev/wp-json/wp/v2/{$uri}";
- }
- function extractPost( $post ) {
- $data = [];
- $content = extractInlineImages($post);
- $data['date'] = $post->created;
- $data['date_gmt'] = $post->created;
- $data['modified'] = $post->modified;
- $data['modified_gmt'] = $post->modified;
- $data['slug'] = $post->slug;
- $data['title'] = $post->title_rus;
- $data['excerpt'] = $post->subtitle_rus;
- $data['content'] = $content;
- $data['status'] = $post->published ? 'publish' : 'draft';
- $data['featured_media'] = $post->featured_media;
- return $data;
- }
- function extractFeaturedImage( $image ) {
- $buff = explode( '/', $image );
- $name = end( $buff );
- $image = str_replace( ' ', '%20', $image );
- return [
- 'file' => file_get_contents( "http://newsmaker.md/{$image}" ),
- 'name' => $name,
- ];
- }
- //Extract images from inline
- function extractInlineImages ($post) {
- $doc=new DOMDocument();
- $html = $post->content_rus;
- $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
- libxml_use_internal_errors(true);
- $doc->loadHTML($html);
- libxml_use_internal_errors(false);
- $images = $doc->getElementsByTagName('object');
- echo '<pre>';
- foreach ($images as $img) {
- $content = $img->textContent;
- $mey = explode(' ', $content);
- $mey = explode(' ', $mey[2]);
- $mey = $mey[0];
- echo $mey . '</br>';
- $node = $doc->createElement('p', 'Value');
- $img->appendChild($node);
- }
- return $doc->saveHTML();
- }
- function printData( $post ) {
- echo '<pre>';
- print_r( $post );
- echo '</pre>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement