• Resolved raz921992

    (@raz921992)


    $newPost = $postClient->request('POST', 'https://www.cutwallpapers.com/wp-json/wp/v2/posts', [
    			'headers' => [
    				'Authorization' => 'Basic ' . base64_encode( $user . ':' . $password ),
    			],
        		'verify' => false,
        		'timeout'  => 2.0,
                'json' => [
                    'title' => 'New title', 
                    'excerpt' => 'testttt', 
                    'content' => 'daaaaa',
                    'status' => 'publish'
                ]
            ]);
    
            $newPostBody = json_decode($newPost->getBody());
    
            $fdata = file_get_contents('https://www.base64-image.de/build/img/mr-base64-482fa1f767.png');
            $media = $postClient->request('POST', 'https://www.cutwallpapers.com/wp-json/wp/v2/media', [
                'headers' => [
                    'Authorization' => 'Basic ' . base64_encode( $user . ':' . $password ),
                    'Content-Type' => 'image/jpg',
                    'Accept' => 'images',
                    'Cache-Control' => 'no-cache',
                    'Content-Disposition' => 'form-data; name='.$data['title'].'; filename='.$data['title']
                ],
                'multipart' => [
                    [
                        'name'     => 'imagineeeee',
                        'contents' => $fdata,
                        'filename' => $data['title'],
                    ],
                ],
                'query' => [
                    'status' => 'publish',
                    'title' => 'File Title!',
                    'comment_status' => 'open',
                    'ping_status' => 'closed',
                    'alt_text' => 'File Alt Text',
                    'caption' => 'File Caption',
                    'description' => 'File Description',
                    'media_type' => 'image',
                    'post' => $newPostBody->id
                ],
            ]);
            $media = json_decode($media->getBody());

    The question is, how can I send image from an external link to a wordpress website by wp API and if the method it’s ok.
    I need a post request to create the post(this is working ok) and after that I send the image on media. It’s any step that I need to do after that?
    In media area I see a blank image only with the right size but I think that the encoded image that I sent it’s not ok.
    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • anant101289

    (@anant101289)

    Hello @raz921992,

    I think there is issue on content-Type, you entered content-type equal to jpg and your image is png. so, please check it.

    please check attched image
    https://prnt.sc/q2sdmy

    Thanks

    Thread Starter raz921992

    (@raz921992)

    Yes this is also a problem but the content-type needs to be a general format(application/json). I read multiple topics on internet and I managed to do it right.

    This is the good version of how to create a post with WP API add feature image and one image on gallery:

    //connection to wordpress api
            $username = 'user';
            $password = 'password';
            $client = new Client([
                'base_uri' => 'https://www.localhost/wp-json/wp/v2/',
                'headers' => [
                    'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
                    'Accept' => 'application/json',
                    'Content-type' => 'application/json',
                    'Content-Disposition' => 'attachment',
                ]
            ]);
            $this->createCategoriesIfNotExist($client);
            // uploading featured image to wordpress media and getting id
            $imageOnMedia = $client->post(
                'media',
                [
                    'multipart' => [
                        [
                            'name' => 'file',
                            'contents' => file_get_contents($data['imageUrl']),
                            'filename' => $data['fileName'],
                        ],
                ],
                'query' => [
                    'status' => 'publish',
                    'title' => $data['originalFileName'],
                    'comment_status' => 'closed',
                    'ping_status' => 'closed',
                    'alt_text' => $data['fileName'],
                    'description' => '',
                    'caption' => '',
        		],
            ]);
            $media = json_decode($imageOnMedia->getBody(), true);
            $categoryId = $client->get('categories', ['query' => ['slug' => $data['deviceCategory']]]);
            if(!empty($categoryId)) {
                $wpcategory = json_decode($categoryId->getBody());
            } else {
                $wpcategory = 1;
            }
    
            $mediaStructure = '<!-- wp:gallery {"ids":['.$media['id'].'],"imageCrop":false,"linkTo":"attachment"} -->
            <figure class="wp-block-gallery columns-1"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><a href="'.$media['link'].'"><img src="'.$media['source_url'].'" alt="werwerwer-1574671862.jpg" data-id="'.$media['id'].'" data-full-url="'.$media['source_url'].'" data-link="'.$media['link'].'" class="wp-image-'.$media['id'].'"/></a></figure></li></ul></figure>
            <!-- /wp:gallery -->';
    
    		// uploading post to wordpress with featured image id
            $post = $client->post(
                'posts', 
                [
                'multipart' => [
                    [
                        'name' => 'title',
                        'contents' => $data['postTitle'],
                    ],
                    [
                        'name' => 'content',
                        'contents' => $mediaStructure
                    ],
                    [
                        'name' => 'featured_media',
                        'contents' => $media['id']
                    ],
                ],
                'query' => [
                    'status' => 'publish',
                    'categories' => [$wpcategory[0]->id]
        		]
            ]);          

    Thank you @anant101289 for your reply!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘API Rest – How to add images inside post and feature image?’ is closed to new replies.