Sample code for 30+ languages & platforms
PHP

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\FileAccess;
use Chilkat2\Http;
use Chilkat2\StringBuilder;

$targetPath = 'c:/temp/qa_output/download.txt';

$http = new Http();
$http->KeepResponseBody = true;

// Assume the target file in the local filesystem does not yet exist.
$http->DownloadAppend('https://chilkatsoft.com/testData/helloWorld.txt', $targetPath);
$statusCode = $http->LastStatus;
if ($statusCode == 0) {
    // Unable to either send the request or get the response.
    echo $http->LastErrorText, "\n";
    exit;
}

// Should be 200.
echo 'Response status code: ', $statusCode, "\n";

// Examine the contents of the file.
$sb = new StringBuilder();
$sb->LoadFile($targetPath, 'utf-8');
echo $sb->GetAsString(), "\n";

// Output:
// Response status code: 200
// Hello World!

// Download another text file and append to the target file.
$http->DownloadAppend('https://chilkatsoft.com/testData/this_is_a_test.txt', $targetPath);
$statusCode = $http->LastStatus;
if ($statusCode == 0) {
    // Unable to either send the request or get the response.
    echo $http->LastErrorText, "\n";
    exit;
}

// Should be 200.
echo 'Response status code: ', $statusCode, "\n";

// Examine the contents of the file.
$sb->LoadFile($targetPath, 'utf-8');
echo $sb->GetAsString(), "\n";

// Output:
// Response status code: 200
// Hello World!This is a Test.

// Delete the local target file.
$fac = new FileAccess();
$fac->FileDelete($targetPath);

// Try to download a file that does not exist:
$http->DownloadAppend('https://chilkatsoft.com/testData/does_not_exist.txt', $targetPath);
$statusCode = $http->LastStatus;
if ($statusCode == 0) {
    // Unable to either send the request or get the response.
    echo $http->LastErrorText, "\n";
} else {
    // We got a response, and we already know it wasn't a 200 success response.
    // It should be a 404 not found.
    echo 'Response status code: ', $statusCode, "\n";
    // Examine the response body.
    echo "Response body:\n";
    echo $http->LastResponseBody, "\n";
}