鍍金池/ 問答/PHP  HTML/ 使用php-phantomjs時在c:\windows寫入出錯

使用php-phantomjs時在c:\windows寫入出錯

在學(xué)習(xí)php-phpantomjs時,使用文檔源碼報錯
public function getEdit(Request $request){

    $client = Client::getInstance();

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

    $request->setMethod('GET');
    $request->setUrl('http://info.sporttery.cn/football/pool_result.php?id=104873');

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

    if($response->getStatus() === 200) {
        echo $response->getContent();
    }
}

貌似是寫入權(quán)限問題,但是為什么會在C:/WINDOWS上寫入呢?怎么解決?圖片描述

回答
編輯回答
紓惘

vendorjonnywphp-phantomjssrcJonnyWPhantomJsDependencyInjectionServiceContainer.php

        self::$instance->setParameter('phantomjs.cache_dir', sys_get_temp_dir());

上面這行換成下面這行,就是緩存目錄改一下,改成可以寫入的目錄

        self::$instance->setParameter('phantomjs.cache_dir', 'd:/');
2017年6月12日 03:53
編輯回答
青裙

哥們,你這個可以動態(tài)輸出內(nèi)容嗎?我的windows上面不能運行,很煩人。

    public function htmlToImage()
    {
        //模擬傳過來的參數(shù)
        $url = request('url', 'http://192.168.0.136:92/htmlToImageView');
        $type = request('type', 1);
        request()->offsetSet('api_token', 'bxs8T3pfy3IfcjftJWBfcNFsvB2Vh0W9Xgr7fww3jYUTuaIn5xFR0fYYr9iJ');
        request()->offsetSet('user_info_id', 1);
        request()->offsetSet('openid', 0);
        // dd(request()->all());

        $client = Client::getInstance();
        $client->isLazy();

        $client->getEngine()->debug(true); //允許或禁止調(diào)試
        // 判斷系統(tǒng)類型來決定執(zhí)行文件的名稱
        $excute = PHP_OS == 'WINNT' ? 'phantomjs.exe' : 'phantomjs';
        $client->getEngine()->setPath(base_path() . '/bin/' . $excute);

        //截取全屏
        $width = 0;
        $height = 0;
        $top = 0;
        $left = 0;

        //唯一文件名
        $name = uniqid();
        //存儲位置
        $storage_path = storage_path('app/public/capture/' . $name . '.jpg');
        if ($type == 1) {
            $request = $client->getMessageFactory()->createCaptureRequest($url, 'GET');
        } else {
            $request = $client->getMessageFactory()->createCaptureRequest($url, 'POST');
            $request->addHeader('X-CSRF-TOKEN', csrf_token());
        }
        //帶上cookie和csrftoken,傳輸?shù)臄?shù)據(jù)
        $request->setRequestData(
            array(
                'api_token' => request('api_token'),
                'user_info_id' => request('user_info_id'),
                'openid' => request('openid'),
            ));

        $request->addHeader('cookie', request()->header('cookie'));

        $request->setTimeout(10000); //超過指定時間則中斷渲染
        $request->setOutputFile($storage_path);
        $request->setViewportSize($width, $height);
        $request->setCaptureDimensions($width, $height, $top, $left);
        $response = $client->getMessageFactory()->createResponse();
        $client->send($request, $response);
        $log = $client->getLog(); #輸出調(diào)試日志
        dd($log, $response->getContent()); #輸出內(nèi)容
        return $this->outPutJson($storage_path);
    }

    public function htmlToImageView()
    {
        $request = request()->all();
        return view('test.htmlToImageView', compact('request'));
    }

視圖文件內(nèi)容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <a href="">測試一下!</a>
    {{Request::url()}}
    {!! QrCode::size(100)->generate(Request::url()); !!}
    @php
        dd($request);
    @endphp
</body>
</html>

clipboard.png

2018年5月23日 16:34