Using PHP, you can take full advantage of PhantomJsCloud by making HTTP Post Requests to our HTTP Endpoint. Below are some examples outlining how to invoke the HTTP Endpoint from PHP.
502 (Bad Gateway)
Errors IMPORTANTIf you use Curl (libCurl) with PHP, you need to set unset the Accept
Header. You
can do so like:
$headers = array( "Accept:" );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
If you do not, you will get 502 Bad Gateway
errors with your curl requests.
The errors won't occur when your POST payload is small, but medium and large POST payloads
will always get this 502
error.
If you follow the examples below you can ignore this suggestion, as these examples use PHP without 3rd party libraries (not using Curl!).
request.json
)
In the following examples, we use PHP to submit POST
requests, sending a
request.json
file as the content.
This file follows either the UserRequest or PageRequest specification,
which can be viewed from our HTTP Endpoint docs.
The default parameters (for pageRequest parameters you do not explicitly set) can be viewed here.
request.json
{
"url":"http://example.com",
"renderType":"plainText"
}
php code
$url = 'http://PhantomJScloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/';
$payload = file_get_contents ( 'request.json' );
$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);
request.json
{
"url":"http://example.com",
"renderType":"plainText",
"outputAsJson":true
}
php code
$url = 'http://PhantomJScloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/';
$payload = file_get_contents ( 'request.json' );
$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 */ }
file_put_contents('capture.json',$result);
request.json
{
"url":"http://www.highcharts.com/stock/demo/intraday-area",
"renderType":"jpeg"
}
php code
$url = 'http://PhantomJScloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/';
$payload = file_get_contents ( 'request.json' );
$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 */ }
file_put_contents('content.jpg',$result);
request.json
{
"url":"https://amazon.com",
"renderType":"pdf"
}
php code
$url = 'http://PhantomJScloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/';
$payload = file_get_contents ( 'request.json' );
$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 */ }
file_put_contents('content.pdf',$result);
request.json
{
"pages":[
{
"url": "http://example.com",
"content": null,
"urlSettings": {
"operation": "GET",
"encoding": "utf8",
"headers": {},
"data": null
},
"renderType": "jpg",
"outputAsJson": true,
"requestSettings": {
"ignoreImages": false,
"disableJavascript": false,
"userAgent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) Safari/534.34 PhantomJS/2.0.0 (PhantomJsCloud.com/2.0.1)",
"authentication": {
"userName": "guest",
"password": "guest"
},
"xssAuditingEnabled": false,
"webSecurityEnabled": false,
"resourceWait": 15000,
"resourceTimeout": 35000,
"maxWait": 35000,
"waitInterval": 1000,
"stopOnError": false,
"resourceModifier": [],
"customHeaders": {},
"clearCache": false,
"clearCookies": false,
"cookies": [],
"deleteCookies": []
},
"suppressJson": [
"events.value.resourceRequest.headers",
"events.value.resourceResponse.headers",
"frameData.content",
"frameData.childFrames"
],
"renderSettings": {
"quality": 70,
"pdfOptions": {
"border": null,
"footer": {
"firstPage": null,
"height": "1cm",
"lastPage": null,
"onePage": null,
"repeating": "%pageNum%/%numPages%"
},
"format": "letter",
"header": null,
"height": null,
"orientation": "portrait",
"width": null
},
"clipRectangle": null,
"renderIFrame": null,
"viewport": {
"height": 1280,
"width": 1280
},
"zoomFactor": 1,
"passThroughHeaders": false
},
"scripts": {
"domReady": [],
"loadFinished": []
}
}
],
"proxy":false
}
php code
function base64_to_jpeg( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file );
}
$url = 'http://PhantomJScloud.com/api/browser/v2/a-demo-key-with-low-quota-per-ip-address/';
$payload = file_get_contents ( 'request.json' );
$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 */ }
$obj = json_decode( $result );
$base64Enc = $obj->{"content"}->{"data"};
base64_to_jpeg( $base64Enc, $obj->{"content"}->{"name"} );
Check out the Advanced Scenario Samples section of the Docs Index Page for Page Automation, Auto-Login, and more.