Solved

PHP CURL and ASPX page

Posted on 2011-04-26
6
1,435 Views
Last Modified: 2012-05-11
Hi,

I've spent quite a bit of time trying to get this to work, but so far to no avail.

I'm attempting to request information from a specific .aspx page.

Using firefox and firebug, I am able to determine the actual url and parameters and if I paste into one of several browsers, the result I am looking is returned satisfactorily:

http://widgets.ghin.com/HandicapLookupResults.aspx?entry=1&dynamic=&small=0&css=default&ghinno=9675575&hidename=0&showmsg=0&showheader=1&showtabheader=0&combinehieff=0&showheadertext=0&showfootertext=0&tab=2

I found I can even shorten this (I assume the other parameters are defaulted) and it works just as well:

http://widgets.ghin.com/HandicapLookupResults.aspx?ghinno=9675575

Now I have read something about the requirement to use VIEWSTATE, which I as understand would be the state from the previous form.  However, as you can see by using the above url's it seems to work fine without going through the form.  However, if it helps anyone understand this better this would be the url to the form:

http://ghin.com/lookup.aspx

and my inserting the number 9675575 in the field you can achieve the same results as above.

I have tried coding this up in a variety if ways without success.  see attached.

I would appreciate any help plus how the solution was thought out.

Thanks


 
<?php
header("Content-type: text/html");


$doc_root_x = $_SERVER['DOCUMENT_ROOT'];



$url = "http://widgets.ghin.com/HandicapLookupResults.aspx";

$entry          = 1;
$dynamic        = "";
$small          = 0;
$css            = "default";
$ghinno         = 9675575;
$hidename       = 0;
$showmsg        = 0;
$showheader     = 1;
$showtabheader  = 0;
$combinehieff   = 0;
$showheadertext = 1;
$showfootertext = 1;
$tab            = 2;

$fields = array(
                        'entry'         =>urlencode($entry),
                        'dynamic'       =>urlencode($dynamic),
                        'small'         =>urlencode($small),
                        'css'           =>urlencode($css),
                        'ghinno'        =>urlencode($ghinno),
                        'hidename'      =>urlencode($hidename),
                        'showmsg'       =>urlencode($showmsg),
                        'showheader'    =>urlencode($showheader),
                        'showtabheader' =>urlencode($showtabheader),
                        'combinehieff'  =>urlencode($combinehieff),
                        'showheadertext'=>urlencode($showheadertext),
                        'showfootertext'=>urlencode($showfootertext),
                        'tab'           =>urlencode($tab)
                );

$fields = array(
                        'ghinno'        =>urlencode($ghinno)
                );                
                
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,"&");


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

//$useragent = $_SERVER["HTTP_USER_AGENT"];
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

$cookie = $doc_root_x . "/joomla/tmp/ghinCookie.txt";
$fp = fopen("$cookie","w") or die("<BR />Unable to open cookie file $cookie for write!<BR />");
fclose($fp);

curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

$result = trim(curl_exec($ch));
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         
curl_close($ch);

echo($result) ;

// possible resutls:
// GHIN Number Invalid


?>

Open in new window

0
Comment
Question by:BeerFizz
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:AnuarJamlus
Comment Utility
If the url doesn't require a post and the page can be retrieved correctly by calling up the full url with parameters, I would think that would be the best way to do it rather than doing a CURL post

 
0
 

Author Comment

by:BeerFizz
Comment Utility

Perhaps, but I need to be able to scrape the page and using curl will allow me to have the result returned to a variable, where I may process the contents.
0
 

Author Comment

by:BeerFizz
Comment Utility

Any other ideas??
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
See http://www.laprbass.com/RAY_temp_beerfizz.php

HTH, ~Ray
<?php // RAY_temp_beerfizz.php
error_reporting(E_ALL);

// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $get_array=array()
, $timeout=3
, $error_report=TRUE
)
{
    // PREPARE THE ARGUMENT STRING IF NEEDED
    $get_string = '';
    foreach ($get_array as $key => $val)
    {
        $get_string .= urlencode($key) . '=' . urlencode($val) . '&';
    }
    $get_string = rtrim($get_string, '&');
    if (!empty($get_string)) $url .= '?' . $get_string;

    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}




// USAGE EXAMPLE - PUT YOUR FAVORITE URL HERE
$url = "http://widgets.ghin.com/HandicapLookupResults.aspx";

// PUT YOUR ARRAY OF KEY=>VALUE PAIRS HERE
$arg = array( 'ghinno' => '9675575' );

// MAKE THE CALL
$htm = my_curl($url, $arg, 2, TRUE);
if (!$htm) die("NO $url");

// SHOW WHAT WE GOT
echo "<pre>";
var_dump($arg);
echo PHP_EOL . $url;
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;

Open in new window

0
 

Author Closing Comment

by:BeerFizz
Comment Utility
Ray,

thank you so much.  It works perfectly.

Phil
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points - it's a great question, and having several URLs to choose from made it easier to get good results.  Cheers, ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now