Aschab
Aschab

Reputation: 1399

phantomjs API error

I'm using https://phantomjscloud.com to generate pdf snapshots of a website. I'm using a really basic example as follows:

$Foptions = new stdClass();
$Foptions->url = 'http://lr.boatsetter.com/boat/?id=1';
$Foptions->renderType = "pdf";

$optionsjson = json_encode($Foptions);

$url = 'http://PhantomJScloud.com/api/browser/v2/myapi/';
$payload = $optionsjson;
$options = array(
    'http' => array(
        'header'  => "Content-type: application/json\r\n",
        'method'  => 'POST',
        'content' => $payload
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
file_put_contents('boat_1.pdf',$result);

This works perfectly when $Foptions->url is amazon.com or google.com. But not on my example. What could be the error?

Upvotes: 0

Views: 117

Answers (1)

Aschab
Aschab

Reputation: 1399

I'm using the http api and its working:

$headers  =  array( "Accept:" );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$wp_upload_dir = wp_upload_dir();
$upload_dir = $wp_upload_dir['basedir'] . '/pdf/';
$url  = 'https://phantomjscloud.com/api/browser/v2/myapi/?request={url:%22'.get_home_url().'/boat/?id='.$boat_id.'%22,renderType:%22pdf%22}';
$path = $upload_dir."boat_".$boat_id.".pdf";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $url);

$data = curl_exec($ch);

curl_close($ch);

$result = file_put_contents($path, $data);
    if(!$result){
        echo "error";
    }else{
        echo "success";
    }

$file = $path;
$filename = 'Boat_'.$boat_id.'.pdf'; 
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);

Upvotes: 0

Related Questions