0

I am trying to submit a form using PHP cURL. Everything works great on localhost but not when I upload it to a server (SiteGround). My code makes an initial get request to get the hidden data needed to make a submission to an asp.net form. Currently, I can't even get the initial page to load that has the form data on it. I receive a 403 Forbidden code in the header (this is new). Previously I was able to submit the form before getting the website's 403 page that loaded the message "If you are experiencing difficulty...", which is where the result would have been loaded. So why would I be able to access the information on localhost but not when I load it on to a server?

$registration = 18217198;

// Make initial cURL to get hidden data in form before making a post request
$url = 'https://www.angus.org/Animal/EpdPedSearch.aspx';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => ['User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'],
));
$html = curl_exec($curl);

if (curl_error($curl)) {
    echo 'Error: ' . curl_error($curl);
}

$doc = new DOMDocument();
$doc->loadHTML($html);

// Traverse to get required data for form submission.
$viewState = $doc->getElementById('__VIEWSTATE')->getAttribute('value');
$hidAniKey = $doc->getElementById('_TSM_HiddenField_')->getAttribute('value');
$eventValidation = $doc->getElementById('__EVENTVALIDATION')->getAttribute('value');
$viewStateGen = $doc->getElementById('__VIEWSTATEGENERATOR')->getAttribute('value');

$postFields = array(
    '__VIEWSTATE' => $viewState,
    '__VIEWSTATEGENERATOR' => $viewStateGen,
    '__EVENTVALIDATION' => $eventValidation,
    'ctl00$MainContent$HidSchDataid' => $hidAniKey,
    'ctl00$cphBodyLeft1$ucEpdPedSearch$uctxtIdentNum' => $registration,
    '__SCROLLPOSITIONX' => 0,
    '__SCROLLPOSITIONY' => 0,
    'ctl00$ucddlSearchType' => 'Animal',
    'ctl00$ucStatesList' => 'noState',
    'ctl00$cphBodyLeft1$ucEpdPedSearch$ucCmbNameSearchOptions' => 0,
    'ctl00$cphBodyLeft1$ucEpdPedSearch$ucbtnSearch' => 'Search'
);

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_HTTPHEADER => ['User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'],
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $postFields,
));
$response = curl_exec($curl);

if (curl_error($curl)) {
    echo 'Error: ' . curl_error($curl);
}
curl_close($curl);

print_r( $response );
Matt S.
  • 1
  • 1
  • 1

0 Answers0