Making POST/PUT Requests
Some advanced users may want to send POST/PUT requests in order to scrape forms and API endpoints directly. You can do this by sending a POST/PUT request through ScraperAPI. The return value will be stringified. So if you want to use it as JSON, you will need to parse it into a JSON object.
API ENDPOINT REQUEST
<?php
$url = "http://api.scraperapi.com?api_key=APIKEY&url=http://httpbin.org/anything";
# POST/PUT Requests
$postData = ["foo" =>"bar"];
$postData = json_encode($postData);
$headers = [
"Content-Type: application/json"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
#Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
#Form POST Request
$postData = ["foo" => "bar"];
$postData = json_encode($postData);
$headers = [ 'Content-Type: application/x-www-form-urlencoded; charset=utf-8', ];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
?>
PROXY MODE
<?php
$postData = ["foo"=>"bar"];
$postData = json_encode($postData);
$headers = [
"Content-Type: application/json"
];
# POST/PUT Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://httpbin.org/ip");
curl_setopt($ch, CURLOPT_PROXY, "http://scraperapi:APIKEY@proxy-server.scraperapi.com:8001");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
puts results
# Form Data POST Request
$postData = ["foo"=>"bar"];
$postData = json_encode($postData);
$headers = [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://httpbin.org/ip");
curl_setopt($ch, CURLOPT_PROXY, "http://scraperapi:APIKEY@proxy-server.scraperapi.com:8001");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
SDK Method
# remember to install the library: composer require scraperapi/sdk
<?php
$client = new ScraperAPIClient("APIKEY");
$postResult = $client->post("http://httpbin.org/ip", ["foo" => "bar"])->raw_body;
$putResult = $client->put("http://httpbin.org/ip", ["foo" => "bar"])->raw_body;
print($putResult);
ASYNC Method
$url = "http://async.scraperapi.com"
#Form POST Request
$postData = json_encode([
"url" => "https://httpbin.org/anything",
"apiKey" => "API_KEY",
"method" => "POST",
"headers" => json_encode([ 'Content-Type' => 'application/x-www-form-urlencoded', ]),
"body" => "foo=bar"
]);
$headers = [ 'Content-Type: application/json', ];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Last updated