cURL acting very strange

北慕城南 提交于 2020-01-06 06:32:09

问题


I'm importing some data from external domains successful with cURL, until I tried with this URI: http://www.airbnb.com/calendar/ical/760186.ics?s=29623a93eb0e693c77591a711f082f06, which is a ics calendar. I can successfully run it on a command line (try for your selves): shell>> curl https://www.airbnb.com/calendar/ical/760660.ics?s=593cc556438a8f0919beb6107b6f508d, so it's not a network issue.

but my php script (that do return other URI) DO NOT return this. or better it return false.

here is the small php

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$ical1= "http://www.airbnb.com/calendar/ical/760186.ics?s=29623a93eb0e693c77591a711f082f06";
echo file_get_contents_curl($ical1);

I do think this has something to do with my apache or php configuration, because it runs on appfog and it run with my old xampp instalation. To resume: all URI worked with old xampp instalation and now just the one from the example fails.

on my phpinfo() i can read:

cURL support enabled
cURL Information 7.24.0
Age 3
Features
AsynchDNS Yes
Debug No
GSS-Negotiate Yes
IDN No
IPv6 Yes
Largefile Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI Yes
krb4 No
libz Yes
CharConv No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host i386-pc-win32
SSL Version OpenSSL/1.0.1c
ZLib Version 1.2.5
libSSH Version libssh2/1.3.0

回答1:


The URL is valid and can be downloaded no problems just by putting the URL in to a browser, so that isn't your problem.

The URL redirects to the same URL but on a secure server, but you have set CURLOPT_FOLLOWLOCATION so that isn't your problem either.

However, the secure URL carries out a javascript redirect to the UK site, and this is your problem. You're pulling back the wrong file.

Try it with the URL https://www.airbnb.co.uk/calendar/ical/760186.ics?s=29623a93eb0e693c77591a711f082f06 instead and see how it goes.




回答2:


I had the same problem and I tried using curl and file_get_contents. I tried the below and it works, I don't know why it works and how it differs from the examples above but it gets the ICAL file perfectly.

The url has to be https or it shows a blank screen.

function file_get_contents_curl($url) { $ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;

}

echo file_get_contents_curl('https://www.airbnb.co.uk/calendar/ical/760186.ics?s=29623a93eb0e693c77591a711f082f06');



来源:https://stackoverflow.com/questions/15095673/curl-acting-very-strange

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!