Sunday 9 December 2018

Curl Lib

PHP Curl Lib
// PUT POST using JSON(PHP)

// PUT
$data = array('username'=>'dog','password'=>'tall');
$data_json = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
// POST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
// Set username and password for http simple authentication
// Command line
curl -i \
-u rabbitmq:rabbitmq \

//PHP lib
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);



// Force download
   $file = "filename.ext";
·          
·         // Quick check to verify that the file exists
·         if( !file_exists($file) ) die("File not found");
·          
·         // Force the download
·         header("Content-Disposition: attachment; filename="" . basename($file) . """);
·         header("Content-Length: " . filesize($file));
·         header("Content-Type: application/octet-stream;");
·         readfile($file);


// Curl lib to do basic http authentication
    $options = array(
        CURLOPT_RETURNTRANSFER => true,   // return web page
        CURLOPT_HEADER         => false,  // don't return headers
        CURLOPT_FOLLOWLOCATION => true,   // follow redirects
        CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
        CURLOPT_ENCODING       => "",     // handle compressed
        CURLOPT_USERAGENT      => "test", // name of client
        CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
        CURLOPT_TIMEOUT        => 120,    // time-out on response
        CURLOPT_HTTPAUTH       => "CURLAUTH_BASIC",  // authentication method
        CURLOPT_USERPWD        => "$token:$secret",  // authentication

    ); 


    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

 

// Set cookie, and store sever response set-cookie in cookie file

 $username = "test";
  $password = "test";
  $url = "https:url";
  $cookie= "cookie/cookie.txt";
  $ch = curl_init();


  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie/'.$cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie/'.$cookie);

  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  if (curl_errno($ch)) die(curl_error($ch));

  $doc = new DOMDocument();
  $doc->loadHTML($response);

// Set verbose to debug curl lib

// CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR, 
// or the file specified using CURLOPT_STDERR.
curl_setopt($handle, CURLOPT_VERBOSE, true);

$verbose = fopen('php://temp', 'w+');
curl_setopt($handle, CURLOPT_STDERR, $verbose);




No comments:

Post a Comment