2015-10-30 27 views
2

嘿,我一直在寻找一个解决方案的问题一段时间了,但目前为止没有运气......基本上,我想要在PHP中使用curl下拉页面内容。而下面的代码php curl回来空的内容

static function getContent($url) { 
    // pull down the content that the url pointing to 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_VERBOSE, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    curl_setopt($curl, CURLOPT_USERAGENT, Constants::$USER_AGENT_CHROME); 
    $cookie = realpath(Constants::$ROOT_DIR . Constants::$COOKIE); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); 
    curl_setopt($curl, CURLOPT_FAILONERROR, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); 
    // curl_setopt ($curl, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem'); 
    $content = curl_exec($curl); 
    curl_close($curl); 
    return $content; 
    } 

和调用该函数使用下列URL总是返回我空的内容,并没有问题,到目前为止,与其他不同的URL(来自不同域),我试过了。

$ url ='https://www.etsy.com/listing/150723421/iretrofone-20-steampunk-silver';

任何原因为什么?

[编辑]我在亚马逊Linux上运行这个脚本,机器上可能会丢失一些东西,使问题暴露出来。迄今为止的两个答案都不适用于我。

[编辑]以下是curl_getinfo输出

{"url":"https:\/\/www.etsy.com\/listing\/150723421\/iretrofone-20-steampunk-silver","content_type":"text\/html; charset=UTF-8","http_code":200,"header_size":737,"request_size":287,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.404801,"namelookup_time":0.028505,"connect_time":0.065447,"pretransfer_time":0.243564,"size_upload":0,"size_download":0,"speed_download":0,"speed_upload":0,"download_content_length":0,"upload_content_length":-1,"starttransfer_time":0.40422,"redirect_time":0,"redirect_url":"","primary_ip":"199.27.79.249","certinfo":[],"primary_port":443,"local_ip":"172.31.29.192","local_port":44605} 

[编辑]下面是详细的输出

* Trying 23.41.253.83... 
* Connected to www.etsy.com (23.41.253.83) port 443 (#0) 
* Initializing NSS with certpath: sql:/etc/pki/nssdb 
* skipping SSL peer certificate verification 
* SSL connection using TLS_RSA_WITH_AES_128_GCM_SHA256 
* Server certificate: 
*  subject: CN=*.etsy.com,OU=Ops,O=Etsy Inc,L=Secaucus,ST=AL,C=US 
*  start date: Feb 17 18:11:39 2015 GMT 
*  expire date: Feb 17 18:11:37 2016 GMT 
*  common name: *.etsy.com 
*  issuer: CN=Verizon Akamai SureServer CA G14-SHA2,OU=Cybertrust,O=Verizon Enterprise Solutions,L=Amsterdam,C=NL 
> GET /listing/150723421/iretrofone-20-steampunk-silver HTTP/1.1 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36 
Host: www.etsy.com 
Accept: */* 
Cookie: uaid=uaid%3DYtum0fFFHW4vd8Fy0IIrtOqKsfXg%26_now%3D1446093672%26_slt%3DDsQSnzXs%26_kid%3D1%26_ver%3D1%26_mac%3DsGZ19jZbFEmxLRCZ87q_mSuvLbRtRjH4LjAYFO74NGg. 

< HTTP/1.1 200 OK 
< Server: Apache 
< Expires: Thu, 19 Nov 1981 08:52:00 GMT 
< Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
< Content-Length: 0 
< X-Cnection: close 
< Content-Type: text/html; charset=UTF-8 
< Date: Fri, 30 Oct 2015 16:20:53 GMT 
< Connection: keep-alive 
* Replaced cookie uaid="uaid%3DYtum0fFFHW4vd8Fy0IIrtOqKsfXg%26_now%3D1446222053%26_slt%3D2FNk-6Hh%26_kid%3D1%26_ver%3D1%26_mac%3DsgAm5o2-yY7aTA7Zt0H4gbSfoCf57mdL9KRraF65fig." for domain etsy.com, path /, expire 1480408753 
< Set-Cookie: uaid=uaid%3DYtum0fFFHW4vd8Fy0IIrtOqKsfXg%26_now%3D1446222053%26_slt%3D2FNk-6Hh%26_kid%3D1%26_ver%3D1%26_mac%3DsgAm5o2-yY7aTA7Zt0H4gbSfoCf57mdL9KRraF65fig.; expires=Tue, 29-Nov-2016 08:39:13 GMT; Max-Age=34186700; path=/; domain=.etsy.com; httponly 
< 
* Connection #0 to host www.etsy.com left intact 
+0

我测试你的代码,我得到了我的本地主机的PHP 5.5的内容也许有一些与您的安装只是尝试在另一台机器 –

回答

0

试试这个代码,它与无饼干工作。

<?php 
function getContent($url) { 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, false); 
$data = curl_exec($curl); 
curl_close($curl); 
return $data; 
} 

$url = 'https://www.etsy.com/listing/150723421/iretrofone-20-steampunk-silver'; 

echo $a = getContent($url); 
?> 
+0

试过代码,仍然没有工作。 – bcbishop

+0

你在你的服务器上安装了curl吗? –