PHP: get contents of the aspx file [closed]
I got next problem. I need to get HTML of several pages. All works great with PHP functions file() or file_get_contents() or CURL.
But doesn't work for one URL!! Here it is (of course, I try to get HTML of non-shortened URL).
I tried all, nothing helps. I can open this page in browswer, it returns 200 status, but... I can't get it's contents! It returns 500 error, when I try to get it via CURL:
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.ypDetectClass..ctor() +47
ASP.immigration_immigrating_ainp_application_forms_aspx..ctor() +26
__ASP.FastObjectFactory_app_web_obqstzij.Create_ASP_immigration_immigrating_ainp_application_forms_aspx() +20
System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +160
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecu开发者_开发技巧tionStep.Execute() +93
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618 "
You must send a User-Agent
HTTP Header in the HTTP request.
Using cURL, you can set the CURLOPT_USERAGENT
option. This works:
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; pt-BR; rv:1.9.2.18) Gecko/20110628 Ubuntu/10.04 (lucid) Firefox/3.6.18' );
curl_setopt( $ch, CURLOPT_URL, 'http://albertacanada.com/immigration/immigrating/ainp-application-forms.aspx' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
Check http://php.net/manual/en/function.curl-setopt.php and this user contributed note too: http://www.php.net/manual/en/function.curl-setopt.php#10692
I am able to retrieve the contents of the page with the command line curl
. So it is very likely that you need to set a user agent in your script.
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
精彩评论