Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php curl Content-Length

You shouldn't have to set the content-length yourself.
If you use cURL to send an HTTP POST, 
it will calculate the content length for you.

If you set the CURLOPT_POSTFIELDS value as an array,
it will automatically submit the request as multipart/form-data and use a boundary. 
If you pass a string, it will use application/x-www-form-urlencoded 
so make sure you pass a urlencoded string to CURLOPT_POSTFIELDS and
not an array since you want form-urlencoded.
Comment

How to get only content-length with CURL PHP?

<?php
/**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
function curl_get_file_size( $url ) {
  // Assume failure.
  $result = -1;

  $curl = curl_init( $url );

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "unknown";
    $status = "unknown";

    if( preg_match( "/^HTTP/1.[01] (ddd)/", $data, $matches ) ) {
      $status = (int)$matches[1];
    }

    if( preg_match( "/Content-Length: (d+)/", $data, $matches ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

  return $result;
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: devilbox make database 
Php :: x-default wpml canonical alternate hreflang 
Php :: laravel artisan command run in route 
Php :: failed to delete data in mysqli using php 
Php :: php count result query 
Php :: how to keep some value on input field 
Php :: yii2 gridview get selected rows 
Php :: remove number after decimal in php 
Php :: Finding Vulnerable Urls 
Php :: Symfony 5 - Customize Twig error templates 
Php :: show dot dot after some words php 
Php :: array filter vs array search php 
Php :: php get image from folder as array 
Php :: php season calculation 
Php :: verta sample jalali laravel problem return object 
Php :: letzten 3 zeichen aus einem string entfernen php 
Php :: how to upload images to sql database php PDO 
Php :: codeigniter apache remove index.php 
Php :: how to hide .php in url 
Php :: laravel facade 
Php :: php sort 
Php :: Email "" does not comply with addr-spec of RFC 2822. 
Php :: unexpected end of file php 
Php :: Append a text string to WooCommerce product title loop 
Java :: javafx center node in gridpane 
Java :: The package java.awt.event is not accessible 
Java :: select a random element from a list java 
Java :: java program to calculate age from date of birth 
Java :: swing disable button 
Java :: java file dialog 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =