/    Sign up×
Community /Pin to ProfileBookmark

How can I import a CSV file from remote Server??

Hi,

I need to import a CSV file from other server, I have a url which generates the CSV, can also be downloaded from the browser.

Now:
When I paste the URL into the browse, I can download it with no problem.
But when I try to import the file to my server, using file_get_content() function or by file_get_content_curl(), I get an error message from the script that generates the csv, saying that the system doesn’t allow the content sent or something like that.

Any suggestions how to solve this problem?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 20.2010 — you might have to use php's ftp functions to get the file onto your server.
Copy linkTweet thisAlerts:
@nuniauthorJul 20.2010 — But the access to the file is through HTTP protocol, this is the link:

http://www.pais.co.il/Chance/Pages/last_Results.aspx?download=1

I suppose they make some sort of authentication or something, but I can't figure what it is.

The MIME type should be "text/csv", but I suppose that file_get_contents() has no problem with different MIME types.
Copy linkTweet thisAlerts:
@NogDogJul 20.2010 — You may have to use the [url=http://php.net/curl]cURL[/url] functions and specify some header info to make it look like a browser request, e.g. setting a User-Agent header and possible an Accept header that would be typical of a common browser.
Copy linkTweet thisAlerts:
@nuniauthorJul 20.2010 — I have used fsockopen, with that code:

[code=php]
function fileSockOpen($host, $request, $param) {
$timeout = 15;
$port = 80;
$data = "";

$sk = fsockopen($host, $port, $errnum, $errstr, $timeout) ;

if (!is_resource($sk)) {
exit("connection fail: ".$errnum." ".$errstr) ;
} else {
fputs($sk, "GET ".$request." HTTP/1.0rn") ;
fputs($sk, "User-Agent: Mozilla/3.0 (compatible; Opera/3.0; Windows 95/NT4)rn");
// fputs($sk, "Referer: www.pais.co.il/Chance/Pages/last_Results.aspx");
fputs($sk, "Host: ".$host ."rn");
fputs($sk, "Accept: */*rn");

while (!feof($sk)) {
$data.= fread($sk, 8192);
$stream_meta_data = stream_get_meta_data($sk); //Added line
if($stream_meta_data['unread_bytes'] <= 0) break; //Added line
}
}
fclose($sk) ;
file_put_contents("pais.csv", $data);
}
[/code]


still, didn't work.

I will try to use cUrl with headers, I hope will come up with something...
×

Success!

Help @nuni spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...