• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1362
  • Last Modified:

Trying to use CURL to open aspx page

Hi -
I am trying to use curl to open an aspx/ashx page so I can post some xml to it -
I can use the following code to open non-aspx (html) pages but when I change the url to any page with an .aspx or .ashx extension it doesn't work -
Any insights appreciated -
I have been fooling with this for a couple of days now -
and feel like I have read a million curl postings here on EE -
Thanks in advance -
Richard

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>


0
rwinnick
Asked:
rwinnick
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
arch-itectCommented:
Are there any client side code in the .aspx file?  
What does it look like when you "wget" the .aspx file?  
Please post it here if you can.

If you don't have wget:

http://unxutils.sourceforge.net/
0
 
Ray PaseurCommented:
Let's not waste time with hypothetical examples.  Please post the ACTUAL URL you're trying to use and the ACTUAL code you're trying to use.

CURL has a lot of intricately detailed moving parts, and complex interactions between the scripts.  There is no "one-size-fits-all" answer.  Every CURL post script is an original work product.  Most of them are successful only after much trial and error.

Best regards, ~Ray
0
 
rwinnickAuthor Commented:
Thanks for the quick replies -
I will get permission to 'publish' the url from the appropriate folks -
and will also get the code together that I have tried -
getting late to catch up with them today -
will follow up early am tomorrow -
thanks again -
R
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Eddie ShipmanAll-around developerCommented:
I have done that before, the thing is you have to do two calls. One to get the __VIEWSTATE and __EVENTVALIDATION values then
use them in the second post.

see code below:
    $ch1 = curl_init();
    curl_setopt($ch1, CURLOPT_HEADER, false);
    curl_setopt($ch1, CURLOPT_URL, "http://www.website/search_page.aspx");
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    $returned = curl_exec($ch1);
    curl_close($ch1);
    unset($ch1);
    /*********************************************
    * Clean up the HTML returned from the search
    * Boy they have some crappy HTML!!!!
    **********************************************/
    $tidy = new tidy;
    $tidy->parseString($returned, $config, 'utf8');
    $tidy->cleanRepair();
    $tidyoutput = tidy_get_output($tidy);
    /*********************************************
    * Now use simple_html_dom to parse out the 
    * results, you can get it here:
    * http://simplehtmldom.sourceforge.net/
    **********************************************/
    $html = new simple_html_dom();
    $html->load($tidyoutput);
    unset($returned);
    unset($tidyoutput);
    /*********************************************
    * Get the __VIEWSTATE and __EVENTVALIDATION
    * values
    **********************************************/
    $vs = $html->getElementById ('__VIEWSTATE');
    $ev = $html->getElementById ('__EVENTVALIDATION');
    $errors = 0;
    if(is_object($ev)) {
      $eventvalidation = $ev->getAttribute("value");
    } else {
     $errors++;
    }
    if(is_object($vs)) {
      $viewstate       = $vs->getAttribute("value");
    } else {
     $errors++;
    }
    if($errors> 0) {
      die("There were errors getting __VIEWSTATE/__EVENTVALIDATION values");
    }
    $html->clear();
    unset($ev);
    unset($vs);    
    unset($errors);
    /*********************************************
    * Now let's fill in the values and search
    **********************************************/
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch1, CURLOPT_URL, "http://www.website/search_page.aspx");
    curl_setopt($ch2, CURLOPT_HEADER, false);
    curl_setopt($ch2, CURLOPT_POST, true);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch2, CURLOPT_POSTFIELDS, array('__VIEWSTATE'=>$viewstate,
                                               '__EVENTVALIDATION'=>$eventvalidation,
                                               'btnSubmit' => 'Go',
                                               'searchvalue'=>$value));
    $returned = curl_exec($ch2);
    // Simple error checking here
    if (!$returned) {
      echo curl_error($ch2); 
    }
    curl_close($ch2);
    unset($eventvalidation);
    unset($viewstate);
    unset($ch2);

Open in new window

0
 
Ray PaseurCommented:
"Two calls..." Yep.  Typically the first is GET and the second is POST.  And typically you need to scrape the contents of the <form> into your local variables for the second call.  That is why I asked rwinnick to post the ACTUAL URL -- because without it, we are just guessing about what might be needed.  Some of the guesses may be obvious, others less so.  But life is too short to waste time guessing when the information can be read off the screen.
0
 
Eddie ShipmanAll-around developerCommented:
Hey, I gave him the tools, including showing how to scrape the items in the page.
0
 
Ray PaseurCommented:
Agree it is a good start.  But what if the form asks for information that goes beyond the scope of the keys of CURLOPT_POSTFIELDS?

That's why I say each such project is a bit of R&D.
0
 
rwinnickAuthor Commented:
Hi Guys -
Could not get client to agree to release info -
So I am awarding points for trying to help :-)
Majority to Eddie for sample code -
Also points to Ray as I know he would have helped if I could have gotten code -
Thanks for the support -
Guess I will just have to keep fooling with this -
RIchard
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now