PHP CURL: examples of usage CURLOPT_READFUNCTION


Read more: PHP: How to use PUT method with CURL

Example 1

<?php

set_time_limit(0);

$url = "http://tutorialspots.com/put.php";
$localfile = "1.mp4";
$fp = fopen($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_setopt($ch, CURLOPT_READFUNCTION , function ($ch, $fp, $len) {     
    $data = fgets($fp,$len);     
    return $data;
});
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
print $http_code;

Example 2

<?php

$boundary = '------WebKitFormBoundaryXVw355Y1Jm6sNM0R';

$str = "--$boundary\r\nContent-Disposition: form-data; name=\"image\"; filename=\"mobile-icon-menu.png\"\r\nContent-Type: image/png\r\n\r\n".
    file_get_contents('mobile-icon-menu.png')."\r\n--$boundary--\r\n";

$url = "http://tutorialspots.com/post.php";
$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_HEADER => false, 
    CURLOPT_RETURNTRANSFER => true, 
     
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => array(  
        'Content-Type: multipart/form-data; boundary=' . $boundary, 
        'Content-Length: ' . strlen($str) 
    ), 
    CURLOPT_READFUNCTION => 'readfunc' ,
    CURLINFO_HEADER_OUT=>1,    
));

 
function readfunc($ch, $fp, $len)
{
    static $pos = 0;  
    global $str;    
    $data = substr($str, $pos, $len);     
    $pos += strlen($data);     
    return $data;
}

echo curl_exec($ch);
$b = curl_getinfo($ch);
var_dump($b);
curl_close($ch);

content file post.php

<?php

var_dump( $_FILES );

Result:

array(1) {
  ["image"]=>
  array(5) {
    ["name"]=>
    string(20) "mobile-icon-menu.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(53) "C:\Users\Administrator\AppData\Local\Temp\php7CC0.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(333)
  }
}
array(27) {
  ["url"]=>
  string(39) "http://tutorialspots.com/post.php"
  ["content_type"]=>
  string(24) "text/html; charset=UTF-8"
  ["http_code"]=>
  int(200)
  ["header_size"]=>
  int(210)
  ["request_size"]=>
  int(200)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.015)
  ["namelookup_time"]=>
  float(0.015)
  ["connect_time"]=>
  float(0.015)
  ["pretransfer_time"]=>
  float(0.015)
  ["size_upload"]=>
  float(531)
  ["size_download"]=>
  float(283)
  ["speed_download"]=>
  float(283)
  ["speed_upload"]=>
  float(531)
  ["download_content_length"]=>
  float(283)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0.015)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(9) "127.0.0.1"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(80)
  ["local_ip"]=>
  string(9) "127.0.0.1"
  ["local_port"]=>
  int(55723)
  ["request_header"]=>
  string(200) "POST /post.php HTTP/1.1
Host: tutorialspots.com
Accept: */*
Content-Type: multipart/form-data; boundary=------WebKitFormBoundaryXVw355Y1Jm6sNM0R
Content-Length: 531
Expect: 100-continue

"
}

Leave a Reply