2

I am looking to use PHP to get a few random paragraphs from this site: http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx

What's the shortest set of code to initiate the Submit button (don't need to fill in the text fields) and parse out only the resultant random text?

Can't seem to get the submit piece to work.

1
  • What have you tried ? What do mean by "initiate" ? Do you mean trigger from the submit button ? Commented Oct 31, 2012 at 14:38

2 Answers 2

1

More flexible way - fetch viewstate and eventvalidation dynamically:

<?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]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Output seems to be including "<span id="tmpl_main_lblPara" class="randomSentence">"
1

Try simply to POST the page. Or GET this url http://watchout4snakes.com/CreativityTools/RandomParagraph/RandomParagraph.aspx?_VIEWSTATE=%2FwEPDwUINTQyOTcxOTkPZBYCZg9kFgQCAg9kFgICAQ9kFgICAQ9kFgQCBw8PFgIeBFRleHRlZGQCCQ8PFgQfAGUeB1Zpc2libGVoZGQCAw8PFgIfAAU4Q29weXJpZ2h0IDIwMDcgd2F0Y2hvdXQ0c25ha2VzLmNvbS4gQWxsIFJpZ2h0cyBSZXNlcnZlZC5kZGTorXr6Gf6R0THMyZRJWZJWtrWHYw%3D%3D&_EVENTVALIDATION=%2FwEWBAKs0KOMDAKf643lBALimIbkBQLx2ZbtAfvuWUuGYHixchu%2FJnnxDjXxcnqg&tmpl%24main%24txtSubject=&tmpl%24main%24txtObject=&tmpl%24main%24btnNew=New+Paragraph

You will need just to fetch span.randomSentence content. This can help to do it.

EDIT
Code I've tested:

$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];
}

3 Comments

How did you get the full URL with viewstate etc?
Posted the form and saw fields in firebug. I see in is not working now. I will provide you more flexible solution if you need. Do you?
See below.. Should work. Tell me if it doesn't. Thats a robust code, add some validation etc there before using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.