Difference between revisions of "Short Notes on PHP"
From PaskvilWiki
(Created page with "== Convert Accented Characters to Non-Accented == <pre>setlocale(LC_ALL, "en_US.utf8"); $translit = iconv("utf-8", "us-ascii//TRANSLIT", $_GET['orig_name']);</pre> '''Note''': ...") |
|||
| Line 27: | Line 27: | ||
// if $response === false, something went wrong | // if $response === false, something went wrong | ||
?></pre> | ?></pre> | ||
| + | |||
| + | == HTTP Post in Pure PHP (without cURL) == | ||
| + | |||
| + | <pre>$data = array('param1' => '1', 'param2' => '2'); | ||
| + | $_data = http_build_query($data); | ||
| + | $options = array | ||
| + | ( | ||
| + | 'http' => array | ||
| + | ( | ||
| + | 'method' => 'POST', | ||
| + | 'header' => "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($_data)."\r\n", | ||
| + | 'content' => $_data | ||
| + | ) | ||
| + | ); | ||
| + | $context = stream_context_create($options); | ||
| + | $data = @file_get_contents('http://www.example.com/post', 0, $context);</pre> | ||
| + | |||
| + | For more information see: [http://www.php.net/manual/en/function.http-build-query.php http_build_query()], [http://www.php.net/manual/en/function.stream-context-create.php stream_context_create()], [http://www.php.net/manual/en/function.file-get-contents.php file_get_contents()]. | ||
Revision as of 11:45, 3 December 2012
Convert Accented Characters to Non-Accented
setlocale(LC_ALL, "en_US.utf8");
$translit = iconv("utf-8", "us-ascii//TRANSLIT", $_GET['orig_name']);
Note: On *nix systems, you can check for supported locales using `locale -a`.
Note: Instead of hard-coding the locale to use, you could also use values of HTTP_ACCEPT_LANGUAGE and HTTP_ACCEPT_CHARSET, sent by the browser, to tailor the input locale.
Upload a File using cURL
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
// notice the '@' ahead of the path!
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
// if $response === false, something went wrong
?>
HTTP Post in Pure PHP (without cURL)
$data = array('param1' => '1', 'param2' => '2');
$_data = http_build_query($data);
$options = array
(
'http' => array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($_data)."\r\n",
'content' => $_data
)
);
$context = stream_context_create($options);
$data = @file_get_contents('http://www.example.com/post', 0, $context);
For more information see: http_build_query(), stream_context_create(), file_get_contents().