{"id":15187,"date":"2026-09-20T05:43:49","date_gmt":"2026-09-20T05:43:49","guid":{"rendered":"https:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=15187"},"modified":"2026-09-30T02:42:32","modified_gmt":"2026-09-30T02:42:32","slug":"%e4%bd%9c%e6%a5%ad%e3%83%a1%e3%83%a2-20260920-php-%e3%81%a7-file_get_contents-%e3%81%8b%e3%82%89-curl-%e3%81%b8%e3%81%ae%e5%a4%89%e6%9b%b4","status":"publish","type":"post","link":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=15187","title":{"rendered":"(\u4f5c\u696d\u30e1\u30e2 20260920) PHP \u3067 file_get_contents \u304b\u3089 curl \u3078\u306e\u5909\u66f4"},"content":{"rendered":"<p>API \u30b5\u30fc\u30d0\u30fc\u3068\u306e\u3084\u308a\u53d6\u308a\u306b\u304a\u3044\u3066\u3001file_get_contents \u306e\u3068\u3053\u308d\u304c\u554f\u984c\u306b\u306a\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u601d\u3048\u3066\u3001curl \u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u305d\u306e\u8a18\u9332\u3067\u3059\u3002\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u9577\u3044\u6642\u9593\u304c\u304b\u304b\u308b\u5834\u5408\u306e\u8a71\u3067\u3059\u3002\u901a\u4fe1\u306f\uff12\u6bb5\u968e\u3042\u3063\u3066\u3001\u305d\u306e\u6700\u521d\u304c PHP \u304b\u3089 PHP \u3067\u3059\u3002\u7d9a\u3051\u3066 PHP \u304b\u3089 Python \u3092\u8d77\u52d5\u3057\u3066 ollama API \u3068\u901a\u4fe1\u3057\u307e\u3059\u3002\u8fd4\u4e8b\u3092\u3082\u3089\u3046\u307e\u3067\u306b\u3044\u307e\u306e\u3068\u3053\u308d\uff12\u3001\uff13\u5206\u5fc5\u8981\u3067\u3059\u3002\u305d\u308c\u304c\u3046\u307e\u304f\u3044\u304b\u306a\u3044\u3002<\/p>\n<p>\u6700\u521d\u306e PHP \u304b\u3089 PHP \u3078\u306e\u901a\u4fe1\u3092\u691c\u8a0e\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u3082\u3068\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b file_get_contents \u3067\u66f8\u3044\u3066\u3044\u307e\u3057\u305f\u3002\u6700\u521d\u306f timeout \u3092\u66f8\u3044\u3066\u3044\u306a\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3042\u3068\u3067\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002timeout \u3092\u66f8\u304d\u52a0\u3048\u308b\u307e\u3067\u306f\u3001\u3061\u3087\u3046\u3069\uff11\u5206\u3067\u5207\u308c\u3066\u3044\u307e\u3057\u305f\u3002PHP \u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u6642\u9593\u306e\u3088\u3046\u3067\u3059\uff08www-data \u304c\u5b9f\u884c\uff09\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">$postdata = http_build_query(array('base64data' => $tmpBase64, 'TAnsAndEtc' => $tmpBase64a));\r\n\r\n$headers = array(\r\n    'Content-Type: application\/x-www-form-urlencoded',\r\n    'Content-Length: '.strlen($postdata)\r\n    );\r\n\r\n$context = stream_context_create([\t\r\n\t'http' => ['method'  => 'POST',\r\n\t\t'header'=>implode( \"\\r\\n\", $headers),\r\n\t\t'ignore_errors' => true,\r\n\t\t'content' => $postdata,\r\n\t\t'timeout' => 300\r\n\t]\t\r\n]);\r\n\r\n$res_base64 = file_get_contents($server . '\/phpSimpleQuestion\/allAnswerTest.php', false, $context);\r\n<\/code><\/pre>\n<p>timeout \u3092\u8ffd\u52a0\u3057\u3066\u591a\u5c11\u6539\u5584\u3055\u308c\u305f\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u308c\u3067\u3082\u304b\u306a\u308a\u306e\u78ba\u7387\u3067\u5931\u6557\u3057\u307e\u3059\u3002\u305d\u3053\u3067 curl \u3092\u4f7f\u3063\u305f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u8a18\u8ff0\u306b\u6539\u3081\u307e\u3057\u305f\u3002\u3053\u308c\u306f TIMEOUT \u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u3001\u7121\u671f\u9650\u306b\u5f85\u3064\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">$param = array('base64data' => $tmpBase64, 'TAnsAndEtc' => $tmpBase64a);\r\n\r\n$url = $server . '\/phpSimpleQuestion\/allAnswerTest.php';\r\n\r\n$ch = curl_init();\r\n\r\ncurl_setopt($ch, CURLOPT_URL, $url);\r\ncurl_setopt($ch, CURLOPT_POST, true);\r\ncurl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\r\ncurl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);\r\ncurl_setopt($ch, CURLOPT_TIMEOUT, 300);\r\n\r\n$res_base64 = curl_exec($ch);\r\n\r\ncurl_close($ch);\r\n\r\n\r\n$res = base64_decode($res_base64);<\/code><\/pre>\n<p>\u4e0a\u8a18\u3082\u78ba\u5b9f\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u307e\u3042\u3060\u3044\u3076\u826f\u304f\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\uff08\u81ea\u5206\u3078\u306e\u6ce8\u610f\u66f8\u304d\uff1aRequestToRemoteServer.php \u306e\u4e2d\u306e\u8a18\u8ff0\u3002\u63a1\u70b9\u5dee\u914d\u30b5\u30fc\u30d0\u30fc\u306e\u30d5\u30a1\u30a4\u30eb\u306a\u306e\u3067\u3001\u3042\u3068\u3067\u5fd8\u308c\u305a\u306b\u4fee\u6b63\u3092\u304b\u3051\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\uff09<\/p>\n<p>\u3064\u3044\u3067\u306a\u306e\u3067\u3001\u3053\u306e\u5148\u306e\u901a\u4fe1\u306e\u3053\u3068\u3092\u8ffd\u8a18\u3057\u307e\u3059\u3002\u4e0b\u8a18\u306e Python \u30b3\u30fc\u30c9\u3092 \u5b9f\u884c\u3057\u3066 ollama API \u3068\u901a\u4fe1\u3057\u307e\u3059\u3002\u3053\u3061\u3089\u3082\u554f\u984c\u306a\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u6642\u3005 ollama \u304b\u3089\u8fd4\u4e8b\u304c\u7121\u3044\u3002\u3044\u307e\u306e\u3068\u3053\u308d import ollama \u3092\u907f\u3051\u3066\u3001requests \u3067\u3084\u3063\u3066\u3044\u307e\u3059\u3002www-data \u3067\u5b9f\u884c\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u3046\u3059\u308b\u3068 import ollama \u3092\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u7279\u5225\u306a\u74b0\u5883\u3092\u7528\u610f\u3059\u308b\uff08activate \u3059\u308b\uff09\u5fc5\u8981\u304c\u3042\u308b\u3068\u601d\u3063\u3066\u907f\u3051\u3066\u3044\u307e\u3059\u3002\u4e0b\u8a18\u3067\u306f\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3093\u3067\u4e00\u7dd2\u306b\u9001\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">import requests\r\nfullpath = foldername + '\/sample.png'\r\n\r\nwith open(fullpath, \"rb\") as f:\r\n  img_base64 = base64.b64encode(f.read()).decode(\"utf-8\")\r\n\r\nurl = \"http:\/\/192.168.100.xxx:11434\/api\/chat\"\r\npayload = {\r\n    \"model\": \"gemma4:12b\",\r\n    \"messages\": [{\r\n        \"role\": \"user\",\r\n        \"content\": \"\u3053\u306e\u753b\u50cf\u306f\u7269\u7406\u5b66\u306e\u554f\u984c\u3067\u3059\u3002\u89e3\u7b54\u3059\u308b\u305f\u3081\u306e\u30d2\u30f3\u30c8\u3092\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002\",\r\n        \"images\": [img_base64],\r\n    }],\r\n    \"stream\": False,\r\n}\r\n\r\nresponse = requests.post(url, json=payload, timeout=300)\r\nprint(response.json()[\"message\"][\"content\"])<\/code><\/pre>\n<p>\u73fe\u72b6\u305d\u308c\u307b\u3069\u5b89\u5b9a\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001import ollama \u304c\u5229\u7528\u3067\u304d\u308b\u7279\u5225\u306a\u74b0\u5883\u3092\u6e96\u5099\u3057\u3066\u3001ollama Python \u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u3092\u8a66\u3057\u305f\u307b\u3046\u304c\u826f\u3044\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u305d\u306e\u6642\u306f\u307e\u305f\u8ffd\u8a18\u3057\u307e\u3059\u3002<\/p>\n<p><strong>20260922<\/strong><\/p>\n<p>\u7e70\u308a\u8fd4\u3057\u3067\u306f\u3042\u308a\u307e\u3059\u304c ollama Python \u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u3092\u4f7f\u3046\u3053\u3068\u3092\u907f\u3051\u3066\u3044\u305f\u7406\u7531\u306f\u3001www-data \u3067\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u304b\u3089\u3067\u3059\u3002www-data \u306f\u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u74b0\u5883\uff08\u3053\u3093\u306a\u8a00\u3044\u65b9\u3067\u826f\u3044\u306e\u304b\u306a\uff1f\uff09\u3067\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u52d5\u304b\u3057\u307e\u3059\u3002\u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u74b0\u5883\u3067\u306f pip install ollama \u3068\u304b\u5b9f\u884c\u3067\u304d\u306a\u304f\u3066\u3001ollama \u30e9\u30a4\u30d6\u30e9\u30ea\u30fc \u304c\u4f7f\u7528\u53ef\u80fd\u306a\u74b0\u5883\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u3068\u306f\u5225\u306b\u7528\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u7528\u610f\u3057\u305f\u3068\u3057\u3066\u3082 www-data \u3067 pyhton \u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u3001\u7279\u5225\u306a\u74b0\u5883\u3067\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u304c\u5206\u304b\u308a\u307e\u305b\u3093\u3002\u305d\u308c\u3067\u3068\u308a\u3042\u3048\u305a www-data \u304b\u3089\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\u306a\u3068\u3053\u308d\u306b\u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u3092\u914d\u7f6e\u3059\u308b\u3053\u3068\u3067\u5bfe\u5fdc\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u30b0\u30ed\u30fc\u30d0\u30eb\u3068\u306f\u5225\u306b\u7528\u610f\u3057\u305f\u74b0\u5883\u3067 ollama \u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30d4\u30fc\u3092 www-data \u304c\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\u306a\u5834\u6240\u306b\u914d\u7f6e\u3057\u305f\u3060\u3051\u3067\u3059\uff08sys.path.insert \u306e\u3068\u3053\u308d\uff09\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\">import sys, os\r\nimport base64\r\n\r\nsys.path.insert(0, '\/\u3069\u3053\u304b\/lib\/python3.12\/site-packages\/') # ollama \u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u306e\u5834\u6240\r\n\r\nfrom ollama import Client, ResponseError, RequestError\r\n\r\ndef generateTextWithOllamaClient(content: str, imageFilePath, hostURL, modelName) -> str | None:\r\n\r\n    client = Client(host=hostURL, timeout=180)\r\n\r\n    try:\r\n        response = client.chat(\r\n            model=modelName,\r\n            messages=[{\r\n                'role': 'user',\r\n                'content': content,\r\n                'images': [imageFilePath]\r\n            }]\r\n        )\r\n        \r\n        # \u5fdc\u7b54\u304b\u3089\u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\r\n        generated_text = response['message']['content'].replace(\"'\", \"\\\\'\")\r\n        if generated_text:\r\n            return generated_text.strip()\r\n        else:\r\n            print(\"\u30a8\u30e9\u30fc: \u30ec\u30b9\u30dd\u30f3\u30b9\u306b 'message' \u30ad\u30fc\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u305b\u3093\u3002\")\r\n            print(\"\u30ec\u30b9\u30dd\u30f3\u30b9\u5185\u5bb9:\", generated_text)\r\n            return None        \r\n        \r\n    except RequestError as e:\r\n        # \u63a5\u7d9a\u30a8\u30e9\u30fc\u306a\u3069\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u81ea\u4f53\u306e\u554f\u984c\r\n        print(f\"\u30a8\u30e9\u30fc: Ollama\u30b5\u30fc\u30d0\u30fc \u3078\u306e\u63a5\u7d9a\u307e\u305f\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u4e2d\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002\")\r\n        print(f\"\u8a73\u7d30: {e}\")\r\n        print(\"Ollama\u30b5\u30fc\u30d0\u30fc\u304c\u8d77\u52d5\u3057\u3066\u3044\u308b\u304b\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u8a2d\u5b9a\uff08\u30d5\u30a1\u30a4\u30a2\u30a6\u30a9\u30fc\u30eb\u7b49\uff09\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002\")\r\n        return None\r\n\r\n    except ResponseError as e:\r\n        # Ollama\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u306e\u30a8\u30e9\u30fc\u5fdc\u7b54 (\u4f8b: \u30e2\u30c7\u30eb\u304c\u898b\u3064\u304b\u3089\u306a\u3044)\r\n        print(f\"\u30a8\u30e9\u30fc: Ollama\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u30a8\u30e9\u30fc\u5fdc\u7b54\u304c\u3042\u308a\u307e\u3057\u305f\u3002\")\r\n        print(f\"\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9: {e.status_code}\")\r\n        print(f\"\u30a8\u30e9\u30fc\u5185\u5bb9: {e.error}\")\r\n        return None\r\n\r\n    except Exception as e:\r\n        # \u305d\u306e\u4ed6\u306e\u4e88\u671f\u305b\u306c\u30a8\u30e9\u30fc\r\n        print(f\"\u4e88\u671f\u305b\u306c\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f: {e}\")\r\n        return None\r\n\r\nhostURL = 'http:\/\/192.168.100.206:11434'\r\nmodelName = 'gemma4:12b'\r\naContent = '\u753b\u50cf\u306f\u7269\u7406\u306e\u7df4\u7fd2\u554f\u984c\u3067\u3059\u3002\u554f\u3044\u3092\u89e3\u304f\u305f\u3081\u306e\u30d2\u30f3\u30c8\u3092\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002\u6b63\u89e3\u306f\u6559\u3048\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002'\r\nimageFile = '.\/cutycapt.png'\r\n\r\ngenerated_text = generateTextWithOllamaClient(aContent, imageFile, hostURL, modelName)\r\n\r\nprint(generated_text)<\/code><\/pre>\n<p>\u3044\u307e\u306e\u3068\u3053\u308d\u3001\u3053\u3061\u3089\u306e\u307b\u3046\u304c\u5931\u6557\u304c\u5c11\u306a\u3044\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u3002\u3067\u3082\u307e\u3060\u5931\u6557\u3059\u308b\u3053\u3068\u306f\u666e\u901a\u306b\u3042\u308b\u306e\u3067\u3001\u3082\u3063\u3068\u7d4c\u9a13\u3092\u7a4d\u3080\u5fc5\u8981\u304c\u3042\u308a\u305d\u3046\u3067\u3059\u3002<\/p>\n<pre><code style=\"color: midnightblue; font-size: 12px; white-space: pre-wrap;\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>API \u30b5\u30fc\u30d0\u30fc\u3068\u306e\u3084\u308a\u53d6\u308a\u306b\u304a\u3044\u3066\u3001file_get_contents \u306e\u3068\u3053 &#8230; <a class=\"more-link\" href=\"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=15187\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-15187","post","type-post","status-publish","format-standard","hentry","category-column"],"_links":{"self":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/15187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15187"}],"version-history":[{"count":66,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/15187\/revisions"}],"predecessor-version":[{"id":15279,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/15187\/revisions\/15279"}],"wp:attachment":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}