我的卷曲到romaji.org有什么问题?

时间:2012-12-04 02:53:59

标签: php curl

页面上设置的charset卷曲是Shift_JIS,lang设置为jp

    function jp_new ($jp_text) 
{
// Begin Curl
$session = curl_init();
//$url1 = "http://nihongo.j-talk.com/index.php";
$url1 = "http://www.romaji.org/index.php";
$parameters = '&text='.urlencode($jp_text).'&save=convert+text+to+Romaji';
$header = array(
"Accept-Language: jp",
"Accept-Charset: Shift_JIS");
// $header[] = "Accept-Language: ja"; 
//$parameters = 'kanji='.urlencode($jp_text).'&converter=spaced&Submit=Translate+Now';
curl_setopt($session, CURLOPT_HTTPHEADER, $header); 
curl_setopt($session, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_URL, $url1);
$jp_page = curl_exec($session); 
curl_close($session);

//$pattern = "/romaji'>(.+?)</s";
$pattern = "/color=\"red\">(.+?)</s";
preg_match_all ($pattern, $jp_page, $result_ro);
return $result_ro[1];

}

我得到一个结果,但它搞砸了,如果我手动从romaji.com提交表单,我会得到相同的结果。结果我得到的时候jptext =“犬猫”是“kou(kigou)(kigou)shin i”

我确定preg匹配只会找到一个匹配,并在正确的位置找到它。但它似乎是某种编码问题,但真的是idk。

类似的卷曲适用于“http://nihongo.j-talk.com/index.php”(注释掉的变量),但似乎他们已禁止我,所以我需要调整它以适应这个新网址romaji.org

更新: 在romaji.org页面上的charset是Shift_JIS,我的页面是UTF-8所以我尝试将curlopt标题添加到curl中,就像现在的代码示例中一样,输出中的结果差别很小,括号中的一个单词是删除后,结果仍然搞砸了。

2 个答案:

答案 0 :(得分:0)

如果从浏览器到脚本获得不同的结果,您的目标是模拟浏览器发送的相同数据。通常不同的是“标题”,包括cookie。

第1部分:捕获标题

  1. 您可以使用各种浏览器的网络监控工具(在Chrome / IE中按F12,或者安装Firebug并找到网络监视器)。开始记录并检查发送的请求。具体来说,查找标题。
  2. 您可以通过“Fiddler
  3. 轻松找到这一点
  4. 检查是否有任何标头,以及随请求发送的任何Cookie。如果是一个cookie,你可能需要倒退并弄清楚hte cookie是如何到达那里的,但是现在,你可以硬编码。
  5. 第2部分:发送标题

    在你的情况下,它可能是“Accept-Charset”和“Accept-Language”标题。你可以用

    来表示这些
    $headers = array(
        "Accept-Language: en-us",
        "Accept-Charset: utf-8");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    

    如果您需要其他标头,请添加到阵列。如果您需要Cookie,follow the documentation或google其他示例。

    注意:我注意到您还设置了“POSTFIELDS”,但实际上并没有指定您发送POST。如果要作为POST发送,请添加以下内容:

    curl_setopt($ch, CURLOPT_POST, true);
    

答案 1 :(得分:0)

简单转换可以解决问题

$jp_text = iconv('UTF-8','Shift_JIS' , $jp_text);

$jp_text = mb_convert_encoding($jp_text, "Shift_JIS", "UTF-8");

如果你跑

header('Content-Type: text/html; charset=utf-8');
$str = "犬猫";
var_dump(jp_new($str));

输出

array (size=1)
  0 => string 'inuneko' (length=7)

Online Demo

修改功能

header('Content-Type: text/html; charset=utf-8');
$str = "犬猫";
var_dump(jp_new($str));

function jp_new($jp_text) {
    $session = curl_init();
    $url1 = "http://www.romaji.org/index.php";
    //$jp_text = iconv('UTF-8','Shift_JIS' , $jp_text);
    $jp_text = mb_convert_encoding($jp_text, "Shift_JIS", "UTF-8");
    $parameters = '&text=' . urlencode($jp_text) . '&save=convert+text+to+Romaji';
    $header = array(
            "Accept-Language: en-US,en;q=0.8",
            "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "Referer: http://www.romaji.org/index.php",
            "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    curl_setopt($session, CURLOPT_HTTPHEADER, $header);
    curl_setopt($session, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_URL, $url1);
    $jp_page = curl_exec($session);
    curl_close($session);

    // $pattern = "/romaji'>(.+?)</s";
    $pattern = "/color=\"red\">(.+?)</s";
    preg_match_all($pattern, $jp_page, $result_ro);
    return $result_ro[1];
}