我期待使用PHP來從該網站獲得一些隨機的段落: http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspxPHP表單提交和解析
什麼是最短的一組編碼,以啓動提交按鈕(不需要填寫文本字段)並僅解析出生成的隨機文本?
似乎無法獲得提交作品。
我期待使用PHP來從該網站獲得一些隨機的段落: http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspxPHP表單提交和解析
什麼是最短的一組編碼,以啓動提交按鈕(不需要填寫文本字段)並僅解析出生成的隨機文本?
似乎無法獲得提交作品。
更靈活的方式 - 獲取視圖狀態和eventvalidation動態:
<?php
$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
$c = curl_exec($ch);
curl_close($ch);
//__VIEWSTATE
$viewstate = '';
//__EVENTVALIDATION
$eventvalidation = '';
// Fetch VIEWSTATE
$p1 = '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="';
$p = strpos($c, $p1);
if ($p !== false) {
$s = substr($c, $p+strlen($p1));
$pcs = explode('"', $s);
if (!empty($pcs[0])) {
$viewstate = $pcs[0];
}
}
// Fetch EVENTVALIDATION
$p1 = '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="';
$p = strpos($c, $p1);
if ($p !== false) {
$s = substr($c, $p+strlen($p1));
$pcs = explode('"', $s);
if (!empty($pcs[0])) {
$eventvalidation = $pcs[0];
}
}
// PUT YOUR OBJECT & SUBJECT HERE
$postvalues = array(
'tmpl$main$txtSubject' => '',
'tmpl$main$txtObject' => '',
'tmpl$main$btnNew' => 'New Paragraph',
);
$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?__VIEWSTATE='.urlencode($viewstate).'&__EVENTVALIDATION='.urlencode($eventvalidation).'&tmpl%24main%24txtSubject='.urlencode($postalues['tmpl$main$txtSubject']).'&tmpl%24main%24txtObject='.urlencode($postalues['tmpl$main$txtObject']).'&tmpl%24main%24btnNew=New%20Paragraph';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
$c = curl_exec($ch);
curl_close($ch);
$t = '<span id="tmpl_main_lblPara" class="randomSentence">';
$p = strpos($c, $t);
if ($p !== false) {
$s = substr($c, $p + strlen($t));
$pcs = explode('</span>', $s);
echo $pcs[0];
}
您只需要獲取span.randomSentence
內容。 This可以幫助做到這一點。
編輯
代碼我測試過:
$url = 'http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?__VIEWSTATE=/wEPDwUINTQyOTcxOTkPZBYCZg9kFgQCAg9kFgICAQ9kFgICAQ9kFgQCBw8PFgIeBFRleHRlZGQCCQ8PFgQfAGUeB1Zpc2libGVoZGQCAw8PFgIfAAU4Q29weXJpZ2h0IDIwMDcgd2F0Y2hvdXQ0c25ha2VzLmNvbS4gQWxsIFJpZ2h0cyBSZXNlcnZlZC5kZGTorXr6Gf6R0THMyZRJWZJWtrWHYw==&__EVENTVALIDATION=/wEWBAKs0KOMDAKf643lBALimIbkBQLx2ZbtAfvuWUuGYHixchu/JnnxDjXxcnqg&tmpl%24main%24txtSubject=&tmpl%24main%24txtObject=&tmpl%24main%24btnNew=New%20Paragraph';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
$c = curl_exec($ch);
curl_close($ch);
$p = strpos($c, '<span id="tmpl_main_lblPara" class="randomSentence">');
if ($p !== false) {
$s = substr($c, $p);
$pcs = explode('</span>', $s);
echo $pcs[0];
}
你嘗試過什麼? 「啓動」是什麼意思?你的意思是從提交按鈕觸發? –