[請益] curl 丟資料到wiktionary上
最近自己想寫一個可以丟資料到wiktionary上
想麻煩大家幫我看看是不是哪邊有問題
例如我想編輯一個字 "淋" 讓他產生一個頁面
會轉成這個碼=>%E6%B7%8B
http://zh.wiktionary.org/w/index.php?title=%E6%B7%8B&action=submit
然後他主要填資料的textbox name="wpTextbox1"
所以參考一些相關程式 寫了以下方法 不過不知道是哪邊有問題
無法正常post資料產生新頁面
<?php
include("LIB_http.php");
$target="http://zh.wiktionary.org/w/index.php?title=%E6%B7%8B&action=submit";
$ref="http://zh.wiktionary.org/w/index.php";
$data_array=http_get($target,$ref);
$data_array['wpTextbox1']="test123";
http_post_form($target,$ref,$data_array);
echo "處理完畢";
?>
// LIB_http.php
<?php
function http_get($target, $ref)
{
return http($target, $ref, $method="GET", $data_array="", EXCL_HEAD);
}
function http_post_form($target, $ref, $data_array)
{
return http($target, $ref, $method="POST", $data_array, EXCL_HEAD);
}
function http($target, $ref, $method, $data_array, $incl_head)
{
# Initialize PHP/CURL handle
$ch = curl_init();
# Prcess data, if presented
if(is_array($data_array))
{
# Convert data array into a query string (ie animal=dog&sport=baseball)
foreach ($data_array as $key => $value)
{
if(strlen(trim($value))>0)
$temp_string[] = $key . "=" . urlencode($value);
else
$temp_string[] = $key;
}
$query_string = join('&', $temp_string);
}
# HEAD method configuration
if($method == HEAD)
{
curl_setopt($ch, CURLOPT_HEADER, TRUE); // No http head
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // Return body
}
else
{
# GET method configuration
if($method == GET)
{
if(isset($query_string))
$target = $target . "?" . $query_string;
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
}
# POST method configuration
if($method == POST)
{
if(isset($query_string))
curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt ($ch, CURLOPT_POST, TRUE);
curl_setopt ($ch, CURLOPT_HTTPGET, FALSE);
}
curl_setopt($ch, CURLOPT_HEADER, $incl_head); // Include head as needed
curl_setopt($ch, CURLOPT_NOBODY, FALSE); // Return body
}
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME); // Webbot name
curl_setopt($ch, CURLOPT_URL, $target); // Target site
curl_setopt($ch, CURLOPT_REFERER, $ref); // Referer value
curl_setopt($ch, CURLOPT_VERBOSE, FALSE); // Minimize logs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to
four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
# Create return array
$return_array['FILE'] = curl_exec($ch);
$return_array['STATUS'] = curl_getinfo($ch);
$return_array['ERROR'] = curl_error($ch);
# Close PHP/CURL handle
curl_close($ch);
# Return results
return $return_array;
}
?>
希望能提供一些寶貴意見
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.16.198
※ 編輯: flypen 來自: 140.109.16.198 (05/06 14:50)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章