Category Archives: Chỉ dẫn lập trình

Auto upload form by cURL and PHP

<?php
    $actionUrl = 'http://server/upload/jup.jsp';
    $post_params['title'] = 'How to upload auto by cURL and PHP';
    $post_params['tag'] = 'curl, php, auto, upload, submit, form, http, post';
    
    # File will be upload
    # First
    $post_params['ufile'] = '@/home/huypv/private/xxx.txt;text/plain';
    /*
——WebXXXFormBoundaryjmXSvBkBa2nO7Z3j
Content-Disposition: form-data; name="ufile"; filename="xxx.txt"
Content-Type: text/plain

CONTENT OF FILE xxx.txt
    */
    
    # And more
    $post_params['ifile'] = '@/home/huypv/private/yyy';
/*
——WebXXXFormBoundaryjmXSvBkBa2nO7Z3j
Content-Disposition: form-data; name="ifile"; filename="yyy"
Content-Type: application/octet-stream

CONTENT OF FILE yyy
*/

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $actionUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    #curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
    
    $result = curl_exec($ch);
    #var_dump( curl_getinfo($ch, CURLINFO_HEADER_OUT));
    curl_close($ch);

Server
<form action="http://server/upload/jup.jsp" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br />
<input type="text" name="tag" /><br />

<input type="file" name="ufile" /><br />
<input type="file" name="ifile" /><br />

<input type="submit" value="Post" />
</form>

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