LogoLogo
OverviewRelease NotesDataPipelineFAQs
PHP
PHP
  • Make Requests with ScraperAPI in PHP
    • Use ScraperAPI Endpoint in PHP
    • Use ScraperAPI Proxy Port in PHP
    • Use ScraperAPI SDK in PHP
    • Make Async Requests with ScraperAPI in PHP
      • How to Use ScraperAPI Async Web Scraping in PHP
      • Use Async ScraperAPI Callbacks in PHP
      • Configure ScraperAPI Parameters in PHP
      • Request Async Batch Scraping with ScraperAPI in PHP
      • Decode Base64 Async Responses in PHP
    • ScraperAPI Structured Data Collection in PHP
      • Amazon Product Page API: Structured Data in PHP
      • Amazon Search API: Structured Data in PHP
      • Amazon Offers API: Structured Data in PHP
      • Amazon Reviews API: Structured Data in PHP
      • Ebay Product Page API: Structured Data in PHP
      • Ebay Search API: Structured Data in PHP
      • Google SERP API: Structured Data in PHP
      • Google News API: Structured Data in PHP
      • Google Jobs API: Structured Data in PHP
      • Google Shopping API: Structured Data in PHP
      • Google Maps Search API: Structured Data in PHP
      • Redfin Agent Details API: Structured Data in PHP
      • Redfin 'For Rent' Listings API: Structured Data in PHP
      • Redfin 'For Sale' Listings API: Structured Data in PHP
      • Redfin Listing Search API: Structured Data in PHP
      • Walmart Search API: Structured Data in PHP
      • Walmart Category API: Structured Data in PHP
      • Walmart Product API: Structured Data in PHP
      • Walmart Reviews API: Structured Data in PHP
    • ScraperAPI Async Structured Data Collection in PHP
      • Amazon Product Page API: Async Structured Data in PHP
      • Amazon Search API: Async Structured Data in PHP
      • Amazon Offers API: Async Structured Data in PHP
      • Amazon Reviews API: Async Structured Data in PHP
      • Ebay Product Page API: Async Structured Data in PHP
      • Ebay Search API: Async Structured Data in PHP
      • Google SERP API: Async Structured Data in PHP
      • Google News API: Async Structured Data in PHP
      • Google Jobs API: Async Structured Data in PHP
      • Google Shopping API: Async Structured Data in PHP
      • Google Maps Search API: Async Structured Data in PHP
      • Redfin Agent Details API: Async Structured Data in PHP
      • Redfin 'For Rent' Listings API: Async Structured Data in PHP
      • Redfin 'For Sale' Listings API: Async Structured Data in PHP
      • Redfin Listing Search API: Async Structured Data in PHP
      • Walmart Search API: Async Structured Data in PHP
      • Walmart Category API: Async Structured Data in PHP
      • Walmart Product API: Async Structured Data in PHP
      • Walmart Reviews API: Async Structured Data in PHP
    • Making POST/PUT Requests with ScraperAPI in PHP
    • Customizing ScraperAPI Requests in PHP
      • Customize Amazon Requests by ZIP Code via ScraperAPI in PHP
      • Customize Cached Results via ScraperAPI in PHP
      • Customize Control Costs with ScraperAPI Parameter in PHP
      • Send Custom Headers with ScraperAPI in PHP
      • Customize Device Type with ScraperAPI in PHP
      • Customize Geotargeted Content Scrape via ScraperAPI in PHP
      • Customize Premium Geotargeted Scrape via ScraperAPI in PHP
      • Customize Header Parameter with ScraperAPI in PHP
      • Customize Premium Residential/Mobile Proxies in PHP
      • Customize JavaScript-Rendered Pages via ScraperAPI in PHP
        • Use Render Instruction Set to Scrape Dynamic Pages in PHP
        • Customize Taking a Website Screenshots via ScraperAPI in PHP
      • Customize Scrape Session-Based Proxies via ScraperAPI in PHP
  • Handle and Process Responses via ScraperAPI in PHP
    • Use API Status Codes to Retry Failed Requests in PHP
    • Customize Output Formats via ScraperAPI Parameters in PHP
      • Request JSON Response via Autoparse Parameter in PHP
      • Request LLM Output Formats with ScraperAPI in PHP
    • Request Response Encoding and Content-Type via ScraperAPI in PHP
  • Dashboard & Billing
    • API Key
    • Credit Usage
    • Delete Account
    • Invoice History
    • Billing Email
    • Billing Address
    • VAT Number
    • Payment Method
    • Cancel Subscription
  • Credits and Requests
  • Monitor Your ScraperAPI Account Information in PHP
  • Documentation Overview
Powered by GitBook

Quick links

  • Homepage
  • Dashboard
  • Pricing
  • Contact Sales

Resources

  • Developer Guides
  • Blog
  • Contact Support
  • Learning Hub
On this page

Was this helpful?

  1. Make Requests with ScraperAPI in PHP

Making POST/PUT Requests with ScraperAPI in PHP

Learn how to send POST and PUT requests using ScraperAPI via sync, proxy, and async modes. Scrape forms, APIs, and handle raw or form data with full control.

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 10 months ago

Was this helpful?