Tag Archives: php

file_get_contents with context

$context = stream_context_create(array(
‘http’ => array(
‘method’ => ‘POST’,
‘header’ => //sprintf(“Authorization: Basic %s\r\n”, base64_encode($username.’:’.$password)).
“Content-type: application/x-www-form-urlencoded; ; charset=UTF-8\r\n” .
“User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6”
,
‘content’ => http_build_query(array(‘q’ => ‘đan ‘)),
‘timeout’ => 5,
),
));
$ret = file_get_contents(‘http://huypv.net/blog/application/index/search’, false, $context);

Nếu chỉ là script

Build mấy cái đơn giản thì chả cần framework mà làm gì, cứ require với include mà giã! Nhớ lại ngày trước toàn include “openDB.php”, include “closeDB.php” ^^ thấy hay phết. Hôm nay thay đổi tí cho nó có không khí (cái gì mãi cũng nhàm :p), tạo hứng thú với cốt đinh

File: connect_db.php

File: index.php

CURLOPT_COOKIEFILE vs CURLOPT_COOKIEJAR

CURLOPT_COOKIEFILE: The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
CURLOPT_COOKIEJAR: The name of a file to save all internal cookies to when the connection closes.

# Sẽ lấy cookie có trong file để gửi request đi
curl_setopt($curl, CURLOPT_COOKIEFILE, ‘cookie_will_be_used.txt’);

# Sẽ lưu cookie server response về
curl_setopt($curl, CURLOPT_COOKIEJAR, ‘cookie_will_be_saved.txt’);

Gửi mail tiếng Việt utf8

Cái này nhớ mang máng là post lâu rồi mà tìm không thấy, take note lại vậy.
<?php
    if (isset($_SERVER['REMOTE_ADDR'])) exit(0);
    if ($argc != 3) {
        print 'Usage: ' . $argv[0] . ' <mail nguoi nhan> <noi dung>' . "\nExample: " . $argv[0] . ' "huyphungvan@gmail.com" "Phùng Văn Huy is now online"';
        exit(1);
    }
    $to = $argv[1];
    $msg = $argv[2];
    $sentSuccess = sendEmail($to, $msg);
    if ($sentSuccess) echo "Sent email to $to";
    else echo "Sent email to $to failed";
    
    
    function sendEmail($to, $message = '', $subject = 'Thư gửi Huy', $header = '') {
        # Thêm header này để thư gửi thuần Việt :-j
        $utf8Header = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
        return mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=', $message, $utf8Header . $header);
    }