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 :: wp_enqueue_script 
Php :: php get post json data 
Php :: laravel query builder select first 
Php :: invalid datetime format laravel 
Php :: composer_update 
Php :: WooCommerce cart API php 
Php :: check if phone number is valid php 
Php :: laravel migration table column nullable 
Php :: twig or 
Php :: how to completely delete php 
Php :: laravel append 
Php :: laravel 8 foreign key 
Php :: laravel fortify 
Php :: laravel 6 auth 
Php :: laravel validation greater than or equal to 
Php :: laravel required_if 
Php :: php array loop 
Php :: php mysql prepare query 
Php :: how unique field in table in phpmyadmin 
Php :: file upload in php mysql 
Php :: how to get attribute value in xml using php 
Php :: how to get match date and month in php 
Php :: how to add javascript to a php file 
Php :: laravel 7 eloquent on delete set null schema 
Php :: get ip address of client php 
Php :: connect another database in wordpress 
Php :: laravel create request 
Php :: Adding JavaScript to a Specific WordPress Post or Page Using Code in the Footer 
Php :: find over array object php find 
Php :: php json decoding as string incorrectly 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =