Reputation: 17
I have a problem running a PHP script that gets data from an external API that's returning image links and storing them as Wordpress posts using the wp_insert_post()
method.
The problem is that the data is massive and the browser is crashing after running the script.
This is my script:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://myapi.com/name');
$result = curl_exec($ch);
curl_close($ch);
$objectCh = json_decode($result,true);
$ch2 = curl_init();
for($i=0;$i < 20;$i++){
$url = 'https://www.myapi.com/api/images/'.$objectCh[chapters][$i][3].'/';
curl_setopt($ch2, CURLOPT_URL,$url );
$result = curl_exec($ch2);
$object = json_decode($result,true);
for($i=0;$i < count($object[images]);$i++){
$comic = array (
'post_content'=> "<img src=\""$object[images]
[$i][1])">",
'post_title' => 'Chapeter 2 !!',
'post_status' => 'publish',
);
wp_insert_post( $comic,true );
/* echo ($object[images][$i][1]);
echo "<br>";*/
}
}
curl_close($ch2);
This is the script I figured out that runs the script many times and changes the script value, but still the browser is crashing. Is there any way to run this program without it crashing?
I'm using a shared hosting.
Upvotes: 0
Views: 143
Reputation: 2011
You could try to use generators. Something like this:
<?php declare(strict_types=1);
final class PostWithImage
{
/** @var string */
private $chaptersUrlApi;
/** @var string */
private $imagesUrlApi;
public function __construct(string $chaptersUrlApi, string $imagesUrlApi)
{
$this->chaptersUrlApi = $chaptersUrlApi;
$this->imagesUrlApi = $imagesUrlApi;
}
public function generate(int $total): Generator
{
$chapters = $this->getChapters();
for ($i = 0; $i < $total; $i++) {
$images = $this->getImagesPerChapter($chapters[$i][3]);
foreach ($images as $image) {
yield $this->makePostWithImage($image->current()[1]);
}
}
}
private function getChapters(): array
{
return $this->makeRequest($this->chaptersUrlApi)->current()['chapters'];
}
private function getImagesPerChapter(string $chapter): Generator
{
yield makeRequest(sprintf($this->imagesUrlApi, $chapter));
}
private function makeRequest(string $url): Generator
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
curl_close($curl);
yield json_decode($result, true);
}
private function makePostWithImage(string $image): array
{
return [
'post_content' => sprintf('<img src="%s">', $image),
'post_title' => 'Chapeter 2 !!',
'post_status' => 'publish',
];
}
}
// Usage:
$postWithImage = new PostWithImage(
$chaptersUrlApi = 'https://myapi.com/name',
$imagesUrlApi = 'https://www.myapi.com/api/images/%s/'
);
foreach ($postWithImage->generate(20) as $post) {
wp_insert_post($post, true);
}
Upvotes: 1