我期待使用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];
}
你尝试过什么? “启动”是什么意思?你的意思是从提交按钮触发? –