所以我使用:https://github.com/jonnnnyw/php-phantomjs
Phantom JS的PHP包装器。我按照说明操作并使用composer来安装包:
php composer require jonnyw/php-phantomjs:2.*
我使用Codeigniter所以我必须在我的课程定义之前声明使用:
use JonnyW\PhantomJs\Client;
class WebScrape extends CI_Controller { require 'vendor/autoload.php'; ..}
然后我使用了这段代码:
$client = Client::getInstance();
$client->setPhantomJs('/var/www/PhantomJS/vendor/bin/');
$request = $client->getMessageFactory()->createRequest('GET', 'http://php.net/manual/en/ref.image.php#56144');
$response = $client->getMessageFactory()->createResponse();
// Send the request
$client->send($request, $response);
echo $response->getStatus();
if($response->getStatus() === 200) {
// Dump the requested page content
echo $response->getContent();
}
我没有错误,但是$response->getStatus();
返回给我0.我不知道如何调试这个,因为我也没有PHP错误。有谁知道如何解决这个问题?
答案 0 :(得分:0)
对于遇到此问题的人,请执行以下操作:
sudo apt-get remove --auto-remove phantomjs
cd /usr/local/share
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs
在$client = Client::getInstance();
$client->setPhantomJs('/usr/local/share/phantomjs');
现在确保require 'vendor/autoload.php';
在use JonnyW\PhantomJs\Client;
应该工作: