简体   繁体   English

php 向 url 发送请求并接收 csv => 需要将此文件保存在我的服务器中

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

my link (URL) is different!!!我的链接 (URL) 不同!!! and does not work with usual method并且不适用于通常的方法

I have tested all methods (fetch, curl, file, get, put), but it does not work.我已经测试了所有方法(fetch、curl、file、get、put),但它不起作用。

I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'我这里有一个类似的 URL:'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我可以在浏览器中打开它并下载一个 csv 文件我需要在 php 中执行此操作并将 csv 文件保存在服务器上

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.首先要注意的是:您的一些 function 调用不正确,这可能意味着您的代码甚至没有开始请求文件。 fopen expects two parameter where you are only giving it one. fopen需要两个参数,而您只给它一个参数。 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.第一个参数是要打开的资源位置,在你的例子中是URL,第二个参数需要的是打开方式,这里你可能需要r ,表示只读。 This results in a resource that you'd need to read using fread , see more on the fopen php.net page .这会生成您需要使用fread阅读的资源,请参阅fopen php.net 页面上的更多信息。

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_get_contents调用本身是正确的,但由于它前面的or会引发某种语法错误,但我猜 or 仅适用于 stackoverflow

file_put_contents expects at least two parameters as well. file_put_contents也需要至少两个参数。 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它的第一个参数是要写入的文件名,在您的情况下,它类似于file.csv ,第二个参数是要写入文件的数据,这是您使用存储在fopen中的结果的地方$page变量,请参阅file_put_contents php.net 页面上的更多信息

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假设 PHP 可以访问该文件(如果我单击它不会打开的链接,可能是一些代理问题或其他问题)以下应该有效

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将PHP cURL POST请求发送到URL,如何接收文件并提供给用户下载? - How to send a PHP cURL POST request to a URL, receive a file and give to the user download? 尝试将php文件中的http get请求发送到服务器并收到http响应 - Trying to send a http get request from a php file to a server and receive a http response 将xml数据发送到服务器,接收并保存到php中的变量中 - Send xml data to Server, Receive it and save into variables in php 客户端生成 CSV 文件,使用 Angular POST 发送,通过 PHP 后端保存服务器端 - Generate CSV file client side, send using Angular POST, save server side through PHP backend 如何将json发送到php文件以保存在服务器上 - how to send json to php file to save on server 需要使用php发送异步url请求并知道所需的时间 - Need to send asynchronous url request using php and know, the time required 如何从Android向PHP服务器localhost发送请求(字符串数据)并从服务器PHP接收响应结果 - How to send request(String data) from android to PHP server localhost and receive response result form server PHP 将表格发送到url并将生成的文档保存在我的服务器上 - Send form to url and save generated document on my server php保存到csv文件 - php save to csv file 从URL下载CSV文件并使用用户名密码保存在服务器上 - Download csv file from url and save on server with username password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM