[英]How to pass GET parameters in curl PHP
I am new to PHP
development. 我是
PHP
开发的新手。 I am working on CURL
to call my WEB API
. 我正在使用
CURL
调用我的WEB API
。 As a newbie I found very hard to understand things. 作为一个新手,我很难理解。
How my API is working 我的API如何运作
API_URL is http://localhost:14909/api/meters/GetByMsn/002999000077/2017-10-11T12:16:20
API_URL是
http://localhost:14909/api/meters/GetByMsn/002999000077/2017-10-11T12:16:20
It takes a meter serial number
and a data time
and gives the response by authorizing
the URL. 它需要一个
meter serial number
和一个data time
并通过authorizing
URL给出响应。 The response I get is 我得到的答复是
{
"data": {
"Response": "No"
}
}
What I want to do 我想做的事
Now In PHP
I am using CURL to make the request. 现在在
PHP
我使用CURL发出请求。 The request is simple as it takes the current selected meter serial number
and a current date time
also it should take authorization key. 该请求很简单,因为它需要使用当前选择的
meter serial number
和current date time
并且还应该使用授权密钥。
What I have done till now 到现在为止我所做的
Below is the code so far i have done 下面是到目前为止我完成的代码
if( isset($_REQUEST['selected_meters']))
{
$m = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
$msn = $m->meter_serial; // current selected meter serial number is saved
$date_time = str_replace(' ','T',date('Y-m-d H:i:s')); // current date time
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'; // my base URL
$curl = curl_init($api_url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // setting the authorization key in header.
exit();
}
Now I want to send the meter serial number
and date time
parameters. 现在,我要发送
meter serial number
和date time
参数。 For this I have searched many articles but all I found a way to pass parameters as query and related link . 为此,我搜索了许多文章,但所有文章都找到了一种将参数作为查询和相关链接传递的方法。
One method I am thinking of is passing parameters direct to the URL Like: 我正在考虑的一种方法是将参数直接传递给URL,例如:
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/[$msn]/[$date_time]';
OR 要么
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/' + $msn + '/'+$date_time;
But I don't know will it works or not 但我不知道它是否会起作用
Any help would be highly appreciated. 任何帮助将不胜感激。
Try this out and see if it works: 试试看,看看是否可行:
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/${msn}/${date_time}';
or 要么
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/{$msn}/{$date_time}';
So after a lot of searching I manage to get a response. 因此,经过大量搜索,我设法获得了答复。 Concatenate both of the parameters in the
URL
and changing the curl_setopt
. 连接
URL
中的两个参数并更改curl_setopt
。
Changes: 变化:
From 从
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'; // my base URL
To 至
$api_url = 'http://116.xx.xx.xx:xxxx/api/meters/GetByMsn/'.$msn . '/' . $date_time; // my base URL
And 和
curl_setopt($curl,CURLOPT_RETURNTRANSFER, CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // setting the authorization key in header.
To 至
curl_setopt($curl CURLOPT_HTTPHEADER, array('Authorization: MY_KEY')); // Removed the CURLOPT_RETURNTRANSFER
And then 接着
$curl_response = curl_exec($curl);
print_r($curl_response);
/* print_r($msn);
echo $date_time;*/
//echo date('Y-m-d H:i:s');
exit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.