简体   繁体   中英

php send request to url and receive csv => need save this file in my server

my link (URL) is different!!! and does not work with usual method

I have tested all methods (fetch, curl, file, get, put), but it does not work.

I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'

I can open it in the browser and download a csv file I need to do this in php and save the csv file on server

Here is what I have tried so far:

$page = fopen('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'); or $page = file_get_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
file_put_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');

First things first: some of your function calls aren't correct, which might mean that your code doesn't even start requesting the file. fopen expects two parameter where you are only giving it one. The first parameter is the resource location to open, in your case it is the URL, the second parameter it requires is the open mode, you'd probably want r here, which means read-only. This results in a resource that you'd need to read using fread , see more on the fopen php.net page .

Your file_get_contents call is correct in itself, but because of the or in front of it it will throw some kind of syntax error, but I am guessing the or is there for stackoverflow only

file_put_contents expects at least two parameters as well. It's first parameter is the filename to write to, which in your case would be something along the lines of file.csv , the second parameter is the data to write to the file, this is where you'd use the results of fopen stored in the $page variable, see more on the file_put_contents php.net page

Assuming PHP has access to the file (if I click the link it won't open, might be some proxy issues or something) the following should work

$page = file_get_contents("http://example.com");
file_put_contents("some_file.csv", $page);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM