Asked  1 Year ago    Answers:  5   Viewed   14 times

I am using file_get_contents to get a headers of an external page to determine if the external page is online like so:

$URL = "http://page.location/";
$Context = stream_context_create(array(
'http' => array(
    'method' => 'GET',
)
));
file_get_contents($URL, false, $Context);
$ResponseHeaders = $http_response_header;

$header = substr($ResponseHeaders[0], 9, 3);

if($header[0] == "5" || $header[0] == "4"){
//do stuff
}

This is working well except when the page is taking too long to respond.

How do I set a timeout?

Will file_get_headers return FALSE if it has not completed yet and will PHP move to the next line if it has not completed the file_get_contents request?

 Answers

1

Add a timeout key inside the stream_context_array

$Context = stream_context_create(array(
'http' => array(
    'method' => 'GET',
    'timeout' => 30, //<---- Here (That is in seconds)
)
));

Your Question....

will file_get_headers return FALSE if it has not completed yet and will PHP move to the next line if it has not completed the file_get_contents request?

Yes , it will return FALSE along with the below warning message as shown.

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Thursday, April 1, 2021
 
1
$DBH = new PDO(
    "mysql:host=$host;dbname=$dbname", 
    $username, 
    $password,
    array(
        PDO::ATTR_TIMEOUT => 5, // in seconds
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    )
);
Thursday, April 1, 2021
 
redrom
 
3

In your url try:

http://user:password@site/ 

(append whatever the rest of the URL for your API should be)

Thursday, April 1, 2021
 
3

Is it timing out before the first row is returned or is it between rows during the read? One good practice bit would be to handle your query in chunks; do a count first to see how many records you are dealing with for the SKU, the loop through smaller chunks (the size of these chunks would depend on how many things you have to do with each row). Your updated workflow would look more like this:

  1. Get next SKU from CSV
  2. Get a total count: SELECT COUNT(*) AS totalCount FROM products WHERE products.sku = csv.sku
  3. Determine chunk size (using 25 for this demo)
  4. loopCount = ceil(totalCount / 25)
  5. Loop through all results using a loop like this: for($i = 0; $i < loopCount; $i++)
  6. Inside your loop you should be running a query like this: SELECT * FROM products WHERE products.sku = csv.sku LIMIT (loopCount*25), 25

You will want to use a constant order for your SELECT chunks; your unique ID would probably be best.

Thursday, April 1, 2021
 
danjah
 
5

Fixed it. In case anyone has the same problem:


$url = "http://some-adress/test.php";
$headers = get_headers($url, 1);
$content_length = $headers["Content-Length"];
$content = file_get_contents($url, NULL, NULL, NULL, $content_length);
echo $content;


Saturday, May 29, 2021
 
Terry
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :