Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Trying to use CURL to open aspx page

Posted on 2010-08-24
Medium Priority
Last Modified: 2013-12-13
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 -

// 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

// close cURL resource, and free up system resources

Question by:rwinnick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 33514916
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:

LVL 111

Expert Comment

by:Ray Paseur
ID: 33515035
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

Author Comment

ID: 33515800
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 -
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 26

Accepted Solution

EddieShipman earned 1200 total points
ID: 34044383
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);
    * Clean up the HTML returned from the search
    * Boy they have some crappy HTML!!!!
    $tidy = new tidy;
    $tidy->parseString($returned, $config, 'utf8');
    $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();
    * values
    $vs = $html->getElementById ('__VIEWSTATE');
    $ev = $html->getElementById ('__EVENTVALIDATION');
    $errors = 0;
    if(is_object($ev)) {
      $eventvalidation = $ev->getAttribute("value");
    } else {
    if(is_object($vs)) {
      $viewstate       = $vs->getAttribute("value");
    } else {
    if($errors> 0) {
      die("There were errors getting __VIEWSTATE/__EVENTVALIDATION values");
    * 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,
                                               'btnSubmit' => 'Go',
    $returned = curl_exec($ch2);
    // Simple error checking here
    if (!$returned) {
      echo curl_error($ch2); 

Open in new window

LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 34046655
"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.
LVL 26

Expert Comment

ID: 34046883
Hey, I gave him the tools, including showing how to scrape the items in the page.
LVL 111

Expert Comment

by:Ray Paseur
ID: 34047157
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.

Author Closing Comment

ID: 34076438
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 -

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question