jonnnnyw / php-phantomjs

Execute PhantomJS commands through PHP
MIT License
1.44k stars 432 forks source link

PhantomJs not working getting blank-page only #254

Open MGA-Coder opened 6 years ago

MGA-Coder commented 6 years ago

hi guys , i'm sorry but this is library very hard every step found error; :( final error to now is getting blank page and not found anything can i found any solve for it this is my php Code , Help me if you can Please i'm tired but i can't find any solve :(


 <?php  
    require 'vendor/autoload.php';

    use JonnyW\PhantomJs\Client;

    $client = Client::getInstance();

        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

    $client->getEngine()->setPath('/home/XXXXX/public_html/bot/bin/');
    $client->getEngine()->debug(true);
   var_dump($client->getLog());

    $request  = $client->getMessageFactory()->createRequest();
    $response = $client->getMessageFactory()->createResponse();

    $request->setMethod('GET');
    $request->setUrl('http://jonnyw.me');

    $client->send($request, $response);

    if($response->getStatus() === 200) {
        echo $response->getContent();
    }
askme-gpt commented 5 years ago
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use JonnyW\PhantomJs\Client;
use Storage;

class GeneratePropagandaController extends Controller
{
    public function htmlToImage($data)
    {
        $url = $this->htmlToImageView($data);
        // $url = 'https://wap.51kid.com/a/free-lesson';
        // $url = 'https://zhidao.baidu.com/question/1241254892809348899.html';
        $client = Client::getInstance();
        if (PHP_OS == 'WINNT') {
            $client->getEngine()->setPath(base_path() . '\\bin\\phantomjs.exe');
        } else {
            $client->getEngine()->setPath(base_path() . '/bin/phantomjs');
        }

        //设置截图的边界
        $width = 0;
        $height = 0;
        $top = 0;
        $left = 0;

        $request = $client->getMessageFactory()->createCaptureRequest($url, 'GET');
        //传输参数
        $request->addHeader('X-CSRF-TOKEN', csrf_token());
        $request->addHeader('cookie', request()->header('cookie'));
        $request->setBodyStyles(array('backgroundColor' => '#FFFFFF')); #设置背景颜色

        $name = 'capture/' . uniqid() . '.jpg';
        $storage_path = Storage::disk('public')->path($name);
        $image_url = Storage::disk('public')->url($name);
        //注意,不论是windows还是linux,这里都需要linux格式的分隔符,不然识别不了
        $storage_path = str_replace('\\', '/', $storage_path);
        $request->setOutputFile($storage_path); #设置输出图片的绝对路径
        $request->setViewportSize($width, $height);
        $request->setCaptureDimensions($width, $height, $top, $left);
        $response = $client->getMessageFactory()->createResponse();
        try {
            $client->send($request, $response);
            return $this->outPutJson([$image_url, $url]);
        } catch (\Exception $e) {
            return $this->outPutJson('', 201, $e->getMessage());
        }
    }

    public function htmlToImageView($data)
    {
        $view = request('view', 'test.experience_report');
        $htmlCode = view($view, compact('data'))->render();
        $fileName = 'capture/html/' . uniqid() . '.html';
        Storage::disk('public')->put($fileName, $htmlCode);
        return Storage::disk('public')->url($fileName);
    }
}
d2x commented 5 years ago

Likely the same issue here: https://github.com/jonnnnyw/php-phantomjs/issues/261#issuecomment-553590291