0
我想从运行在nginx服务器上的php代码发送请求到另一个远程django服务器。我期望使用序列化器在REST API中获取数据。php发送获取请求信息
首先,如果我打开我的本地浏览器链接:
我会得到:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": "000e3588-9544-4df6-a589-cc0166242b5b",
"docfile": "/media/documents/2017/06/30/DSC03623.JPG"
},
{
"id": "3dc6be9f-8659-41d8-8282-b64662032da6",
"docfile": "/media/documents/2017/06/30/DSC03611_9KWbftQ.JPG"
},
{
"id": "28eacb2d-0798-46e9-b63e-10ff704482ce",
"docfile": "/media/documents/2017/06/30/DSC03555.JPG"
}
]
而这也正是我想,当信息我运行我的PHP代码。
这是我工作的,注释的部分是以前失败的尝试:
<?php
echo "<h1>I am here</h1>";
$url = "http://192.168.2.0:8000/myapp";
/*$body = http_parse_message(http_get($url))->body;
$body = http_get($url);
echo $body;*/
/*$client = new Client($url);
$request = $client->newRequest('/documents/');
$response = $request->getResponse();
echo $response->getParsedResponse();*/
$r = new HttpRequest('http://192.168.2.0:8000/myapp/documents/', HttpRequest::METH_GET);
$r->send();
echo $r->getResponseCode();
if ($r->getResponseCode() == 200)
{
echo "<h2>we get 200 response</h2>";
}
?>
打印任何东西,除了第一回声上述3次的非
我在这里
好吧,首先加入一个ELSE给你,如果'else else {echo'actual response ='。 $ R-> getResponseCode(); }'看看你做什么回来 – RiggsFolly
我加了它并没有显示任何内容。 为什么我没有添加一个else语句int首先是因为我正在监视另一台PC上的django服务器。当我运行php代码时,没有请求到达django。好像没有创建http连接 –
那么告诉我们你看到的是在其他服务器上报告的内容 – RiggsFolly