我正在开发一个应该使用file_get_contents函数的php脚本。
当我在我的脚本中使用它只是一次它工作得很棒并且我没有得到任何错误但是如果我实现它更多然后一旦我得到以下错误:
“https://engine.sighten.io/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/): 无法打开流:HTTP请求失败! HTTP / 1.1 400 BAD REQUEST“。
以下是详细信息:
{ "owned_by_user":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "date_completed":"None", "date_created":"2017-08-16T12:47:17.700198+00:00", "date_started":"None", "milestone":{ "link":"/api/solar/milestone/eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "uuid":"eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "natural_id":"API TEST 1 Test Ignore//Cash // Sales Requirements" }, "natural_id":"Installation Agreement", "status":"SIGN", "created_by":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "documents":[ { "owned_by_user":{ }, "date_created":"2017-08-16T12:49:49.245223+00:00", "created_by":{ }, "download_url":"/api/ops/download_from_remote/69c6a394-310a-4300-a18b-7544edfa28a9/", "modified_by":{ }, "uuid":"69c6a394-310a-4300-a18b-7544edfa28a9", "description":"Sun Us Solar Agreement v3.pdf", "natural_id":"Sun Us Solar Agreement v3.pdf", "date_updated":"2017-08-16T12:49:49.245248+00:00", "owned_by_organization":{ }, "remote_host":"AMZ", "name":"Sun Us Solar Agreement v3.pdf", "merged_doc":true }, { "owned_by_user":{ }, "date_created":"2017-08-16T12:49:54.873908+00:00", "created_by":{ }, "download_url":"/api/ops/download_from_remote/7737b88f-0dec-4917-bf73-aa52e3c5e6ab/", "modified_by":{ }, "uuid":"7737b88f-0dec-4917-bf73-aa52e3c5e6ab", "description":"Sun Us Solar Agreement v3.pdf", "natural_id":"Sun Us Solar Agreement v3.pdf", "date_updated":"2017-08-16T12:49:54.873936+00:00", "owned_by_organization":{ }, "remote_host":"AMZ", "name":"Sun Us Solar Agreement v3.pdf", "merged_doc":true }, { "owned_by_user":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "date_created":"2017-08-16T13:03:42.708770+00:00", "created_by":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "download_url":"/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/", "modified_by":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "uuid":"1644d83f-675b-42f9-a8cc-b2cb63d9c705", "description":"Harry Potter and the Sorcerer's Stone.pdf", "natural_id":"Harry Potter and the Sorcerer's Stone.pdf", "date_updated":"2017-08-16T13:03:42.708799+00:00", "owned_by_organization":{ "link":"", "uuid":"e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id":"Sunus Solar" }, "remote_host":"AMZ", "name":"Harry Potter and the Sorcerer's Stone.pdf", "merged_doc":true } ], "modified_by":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "uuid":"411143d0-ecde-429d-943f-d4f22f12c87c", "type":"DOC", "assigned_to":{ "link":"", "uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id":"jbantock@catalystgroupdevelopment.com" }, "date_updated":"2017-08-16 13:03:42.768407+00:00", "date_approved":"None", "owned_by_organization":{ "link":"", "uuid":"e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id":"Sunus Solar" }, "comments":[ ], "site_id":"ac396b97-b22a-4914-9a0f-dbbda61116eb", "name":"Installation Agreement" }
foreach($documents as &$document) {
$downloadlink = $document['download_url'];
$downloadname = $document['name'];
$remote_url = 'https://engine.sighten.io' . $downloadlink;
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Authorization: Token cd048c7b0a127d876e5481ccbd0beb1566bebea2"
)
);
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
$download = file_put_contents($downloadname, $file);
}
$remote_url
变量包含文件的正确URL。澄清:
当我执行脚本时,我看到以下内容 Files
问题是第二个文件没有内容。请注意,我已经使用其他文件再次执行了脚本,因此在这种情况下json如下:
{"date_created": "2017-08-16T15:10:19.327922+00:00", "status": "SIGN", "owned_by_user": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "documents": [{"download_url": "/api/ops/download_from_remote/b608171b-4cd3-4b2a-bef0-19635b00ed52/", "merged_doc": true, "owned_by_user": {}, "date_updated": "2017-08-16T15:10:45.039007+00:00", "uuid": "b608171b-4cd3-4b2a-bef0-19635b00ed52", "remote_host": "AMZ", "description": "Sun Us Solar Agreement v3.pdf", "created_by": {}, "date_created": "2017-08-16T15:10:45.038979+00:00", "modified_by": {}, "owned_by_organization": {}, "name": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf"}, {"download_url": "/api/ops/download_from_remote/049957a5-1340-4e47-bfa2-3e4832d8b88f/", "merged_doc": true, "owned_by_user": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "date_updated": "2017-08-16T15:17:21.529021+00:00", "uuid": "049957a5-1340-4e47-bfa2-3e4832d8b88f", "remote_host": "AMZ", "description": "Harry Potter and the Sorcerer's Stone.pdf", "created_by": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "date_created": "2017-08-16T15:17:21.528992+00:00", "modified_by": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "owned_by_organization": {"link": "", "natural_id": "Sunus Solar", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094"}, "name": "Harry Potter and the Sorcerer's Stone.pdf", "natural_id": "Harry Potter and the Sorcerer's Stone.pdf"}], "date_updated": "2017-08-16 15:17:21.584150+00:00", "date_approved": "None", "modified_by": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "type": "DOC", "created_by": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "comments": [], "uuid": "0f94a08f-4f06-444c-babe-f32ed059a88f", "assigned_to": {"link": "", "natural_id": "jbantock@catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "owned_by_organization": {"link": "", "natural_id": "Sunus Solar", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094"}, "date_started": "None", "name": "Installation Agreement", "date_completed": "None", "natural_id": "Installation Agreement", "milestone": {"link": "/api/solar/milestone/1b599fcd-448f-4444-a636-1ae931f25461", "natural_id": "API TEST 1 Test Ignore//Cash // Sales Requirements", "uuid": "1b599fcd-448f-4444-a636-1ae931f25461"}, "site_id": "c2504124-55e4-43b5-9a0c-370ec494d887"}
答案 0 :(得分:0)
我无法访问您的请求链接,因为我没有API密钥,
当我在我的脚本中使用它只是一次它工作得很好而且我没有得到任何错误但是如果我实现它更多然后一旦我收到错误
你确定你有一个没有API限制(如一秒一个请求)吗?
file_get_contents和file_put_content有时说同步他们完成了他们的工作,实际上硬盘仍在写内容,我有类似的问题,我发现它有效,如果我调用该函数两次,但在中间有4s睡眠
希望这会有所帮助。
答案 1 :(得分:0)
这个脚本对我有用。它没有保存到文件,因为上面的讨论显示TS无法读取第3个文件,因此这不会是问题
在foreach循环结束时,一些变量被转储到屏幕以进行调试。
<?php
error_reporting(E_ALL); ini_set('display_errors', true);
$d = '{"owned_by_user": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "date_completed": "None", "date_created": "2017-08-16T12:47:17.700198+00:00", "date_started": "None", "milestone": {"link": "/api/solar/milestone/eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "uuid": "eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "natural_id": "API TEST 1 Test Ignore//Cash // Sales Requirements"}, "natural_id": "Installation Agreement", "status": "SIGN", "created_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "documents": [{"owned_by_user": {}, "date_created": "2017-08-16T12:49:49.245223+00:00", "created_by": {}, "download_url": "/api/ops/download_from_remote/69c6a394-310a-4300-a18b-7544edfa28a9/", "modified_by": {}, "uuid": "69c6a394-310a-4300-a18b-7544edfa28a9", "description": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf", "date_updated": "2017-08-16T12:49:49.245248+00:00", "owned_by_organization": {}, "remote_host": "AMZ", "name": "Sun Us Solar Agreement v3.pdf", "merged_doc": true}, {"owned_by_user": {}, "date_created": "2017-08-16T12:49:54.873908+00:00", "created_by": {}, "download_url": "/api/ops/download_from_remote/7737b88f-0dec-4917-bf73-aa52e3c5e6ab/", "modified_by": {}, "uuid": "7737b88f-0dec-4917-bf73-aa52e3c5e6ab", "description": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf", "date_updated": "2017-08-16T12:49:54.873936+00:00", "owned_by_organization": {}, "remote_host": "AMZ", "name": "Sun Us Solar Agreement v3.pdf", "merged_doc": true}, {"owned_by_user": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "date_created": "2017-08-16T13:03:42.708770+00:00", "created_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "download_url":"/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/", "modified_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "uuid": "1644d83f-675b-42f9-a8cc-b2cb63d9c705", "description": "Harry Potter and the Sorcerer\'s Stone.pdf", "natural_id": "Harry Potter and the Sorcerer\'s Stone.pdf", "date_updated": "2017-08-16T13:03:42.708799+00:00", "owned_by_organization": {"link": "", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id": "Sunus Solar"}, "remote_host": "AMZ", "name": "Harry Potter and the Sorcerer\'s Stone.pdf", "merged_doc": true}], "modified_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "uuid": "411143d0-ecde-429d-943f-d4f22f12c87c", "type": "DOC", "assigned_to": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock@catalystgroupdevelopment.com"}, "date_updated": "2017-08-16 13:03:42.768407+00:00", "date_approved": "None", "owned_by_organization": {"link": "", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id": "Sunus Solar"}, "comments": [], "site_id": "ac396b97-b22a-4914-9a0f-dbbda61116eb", "name": "Installation Agreement"}';
$documents = json_decode($d);
$documents = $documents->documents;
foreach($documents as $document) {
$downloadlink = $document->download_url;
$downloadname = $document->name;
$remote_url = 'https://engine.sighten.io' . $downloadlink;
$opts = array('http' => array('method' => "GET", 'header' => "Authorization: Token cd048c7b0a127d876e5481ccbd0beb1566bebea2"
));
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
$size = strlen($file);
if(1) {
echo 'on line: ' . __LINE__ . ' in file ' . __FILE__;
printf('<pre>%s</pre>', print_r($document->download_url, 1));
printf('<pre>%s</pre>', print_r($downloadname, 1));
printf('<pre>%s</pre>', print_r($size, 1));
}
} ?>
您确定在真实脚本中发送了Auth代码吗?