1、使用 file_get_contents() 函數(shù), 參數(shù)為域名地址時返回空,為ip地址時有數(shù)據(jù)。
2、linux中可以ping 通 域名
3、php-cli 執(zhí)行,可以訪問遠(yuǎn)程域名,并到得數(shù)據(jù)。
4、通過瀏覽器 即php-fpm模式 curl 和 file_get_contents 都不可以訪問遠(yuǎn)程域名。
5、在 /etc/hosts 中添加 220.181.112.244 www.baidu.com 后 php-fpm模式,就可以訪問到數(shù)據(jù)了。
代碼:
echo 1;
try {
var_dump(file_get_contents('http://220.181.112.244/index.html'));
// var_dump(file_get_contents('http://www.baidu.com'));
} catch (Exception $e) {
print_r($e->getTrace());
}
echo 2;
1、參數(shù)為:http://www.baidu.com
2、參數(shù)為:http://220.181.112.244/index....
3、在服務(wù)器執(zhí)行腳本,兩種參數(shù)都有返回數(shù)據(jù)。
這是file_get_contents() 方法請求的錯誤信息:
2018/05/26 22:16:03 [error] 24942#0: *46192 FastCGI sent in stderr: "PHP message: PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/com.9b/duolaixue_admin/test_print/test.php on line 13
php curl 請求的錯誤信息:
Could not resolve host: www.baidu.com; Name or service not known
1、如果你是將你的上面代碼放到服務(wù)器運(yùn)行的話,百度的服務(wù)器可能會識別你USERAGENT為機(jī)器人robot,拒絕你的訪問
2、file_get_contents()某些時候是可行的,但是有些特殊情況也可以使用curl庫的函數(shù),為了防止對方識別你為robot,可以將服務(wù)器偽裝訪問的瀏覽器用戶是Mozilla/4.0,比如以下代碼
<?php
$curl = curl_init();
//這里設(shè)置你的USERAGENT
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$xxx = curl_exec($curl);
curl_close ($curl);
echo $xxx;
?>
curl相關(guān)的參數(shù)可見:http://php.net/manual/en/func...
補(bǔ)充:
關(guān)于你的問題,這段代碼我是運(yùn)行成功的
<?php
$url = "http://www.baidu.com";
$page = "/services/calculation";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
?>
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。