试图将URL与PHP CURL连接起来,但没有成功

时间:2013-11-18 11:54:39

标签: php url curl

这是我的网址:https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML

我试图通过PHP curl获取其内容,但没有成功希望有些人可以提供帮助。

这是我的代码:

// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php 
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_POST, TRUE);             // Use POST method
//curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=1&var2=2&var3=3");  // Define POST values
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);

$output = curl_getinfo($ch);
print_r($output);

curl_close($ch);
return $data;
}

$url = "https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML" ;


$variablee = get_data($url);
echo $variable;

2 个答案:

答案 0 :(得分:0)

我正在按预期获得结果。

应该是

$variablee = get_data($url);
echo $variablee; //<--- Must be $variablee (as you have defined it above)

<强>输出:

  

数组([url] =&gt;   https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML   [content_type] =&gt;文本/无格式; charset = utf-8 [http_code] =&gt; 200   [header_size] =&gt; 81 [request_size] =&gt; 192 [filetime] =&gt; -1   [ssl_verify_result] =&gt; 19 [redirect_count] =&gt; 0 [total_time] =&gt; 1.544   [namelookup_time] =&gt; 0 [connect_time] =&gt; 0.343 [pretransfer_time] =&gt;   1.186 [size_upload] =&gt; 0 [size_download] =&gt; 656 [speed_download] =&gt; 424 [speed_upload] =&gt; 0 [download_content_length] =&gt; 656   [upload_content_length] =&gt; 0 [starttransfer_time] =&gt; 1.544   [redirect_time] =&gt; 0 [certinfo] =&gt; Array()[primary_ip] =&gt;   196.34.30.23 [primary_port] =&gt; 9443 [local_ip] =&gt; 192.168.72.37 [local_port] =&gt; 61090 [redirect_url] =&gt; )   8310240031083CLAIRECSAPLARN1983 / 10 / 24FY2012 / 03/242013 / 09/308 SKALIE   STWEST ACRES EXT 1312012013/08/130768333118

答案 1 :(得分:0)

function get_data($url) {
$ch = curl_init($url);
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; 
Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 404) {
curl_close($ch);
return '404';
} else {
curl_close($ch);
return $data;
}
}

这个功能对我来说很有效,也许有人觉得它很有用。我不是假装回答你的问题(好吧,我已经晚了9个月),但解决了人们对类似问题的类似问题。