我试图在没有运气的情况下执行cURL。如果我访问网址:
然后我可以看到一个XML文档。
但是,使用PHP 5.6,不会返回任何内容。特别是,我使用以下代码:
<?php
$ch = curl_init("http://webgis.dor.wa.gov/webapi/addressrates.aspx?output=xml&addr=416+Sid+Snyder+Ave+SW&city=Olympia&zip=98504");
curl_setopt_array($ch,array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10
));
$result = curl_exec($ch);
if(!$result)
{
echo 'Curl error: ' . curl_error($ch);
}
$info = curl_getinfo($ch);
echo "Info: ";
print_r($info);
echo "Result: ";
var_dump($result);
结果为:
Info: array(26) {
["url"]=> string(110) "http://webgis.dor.wa.gov/webapi/addressrates.aspx?output=xml&addr=416+Sid+Snyder+Ave+SW&city=Olympia&zip=98504" ["content_type"]=> string(8) "text/xml"
["http_code"]=> int(200)
["header_size"]=> int(236)
["request_size"]=> int(141)
["filetime"]=> int(-1)
["ssl_verify_result"]=> int(0)
["redirect_count"]=> int(0)
["total_time"]=> float(0.654618)
["namelookup_time"]=> float(0.004215)
["connect_time"]=> float(0.064022)
["pretransfer_time"]=> float(0.064091)
["size_upload"]=> float(0)
["size_download"]=> float(369)
["speed_download"]=> float(369)
["speed_upload"]=> float(0)
["download_content_length"]=> float(369)
["upload_content_length"]=> float(-1)
["starttransfer_time"]=> float(0.654568)
["redirect_time"]=> float(0)
["redirect_url"]=> string(0) ""
["primary_ip"]=> string(14) "198.105.130.18"
["certinfo"]=> array(0) { }
["primary_port"]=> int(80)
["local_ip"]=> string(14) "162.244.253.84"
["local_port"]=> int(54442) }
Result: string(369) ""
答案 0 :(得分:2)
试试这个,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://webgis.dor.wa.gov/webapi/addressrates.aspx?output=xml&addr=416%20Sid%20Snyder%20Ave%20SW&city=Olympia&zip=98504",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
// convert it to array if you want
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json,true);
print '<pre>';
print_r($arr);
print '</pre>';
}
答案 1 :(得分:0)
始终检查卷曲请求是否成功。不要以为它成功了。
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}