Php 使用curl将名称-值对发布到URL

Php 使用curl将名称-值对发布到URL,php,post,curl,Php,Post,Curl,我已经注册了一个基于网络的短消息服务,以发送文本消息确认网络表单提交。我正在使用CURL,我的PHP代码如下 $url = "http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx?"; $param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text="; $smsmessage = "Hello,

我已经注册了一个基于网络的短消息服务,以发送文本消息确认网络表单提交。我正在使用CURL,我的PHP代码如下

$url = "http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx?";
$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=";
$smsmessage = "Hello, your table booking for " . $bookingdate . " at " . $booking_time . " is confirmed " , " Myrestaurant";

$ch = curl_init() or die(curl_error()); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$param); 
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$data1=curl_exec($ch) or die(curl_error());
curl_close($ch); 
但它似乎没有向URL发布任何内容(mysmsservice告诉我,日志中没有显示任何传入的请求)。但是,如果我访问下面的URL并替换相应的变量,服务就会工作

&password=MyPassword&to=447710123454471054321&text=TheMessage


不确定我是否正确使用了CURL调用。任何帮助都将不胜感激。提前谢谢。

也许短信服务说当信息丢失时没有有效的请求。如果您在此处查看代码:

$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=";
您永远不会将消息添加到$param。不过,您可以在变量$smsmessage中构建它。您应该将代码修改为:

$smsmessage = "Hello, your table booking for " . $bookingdate . " at " . $booking_time . " is confirmed, " . " Myrestaurant";
$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=" . $smsmessage;

也许短信服务说当信息丢失时没有有效的请求。如果您在此处查看代码:

$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=";
您永远不会将消息添加到$param。不过,您可以在变量$smsmessage中构建它。您应该将代码修改为:

$smsmessage = "Hello, your table booking for " . $bookingdate . " at " . $booking_time . " is confirmed, " . " Myrestaurant";
$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=" . $smsmessage;

如果您说如果您访问页面时直接在地址栏中输入了所有参数(作为GET参数),那么这意味着您不需要进行POST调用

在这种情况下,您甚至不需要使用cURL:

$base='1!'http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx';
$params=数组(
“用户名”=>$username,
“密码”=>$password,
'至'=>$diner\u mobile,
'text'=>'您的预订已确认',
);
$url=sprintf('%s?%s',$base,http_build_query($params));
$response=file\u get\u contents($url);
但是,如果您确实需要使用POST,这应该可以:

$curl=curl_init();
curl_setopt_数组($curl,数组(
CURLOPT_URL=>$base,
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>$params,
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_SSL_VERIFYHOST=>0,//如果需要从https获取,可以避免SSL问题
CURLOPT_SSL_VERIFYPEER=>0,//相同^
));
$response=curl\u exec($curl);

注意:我显然没有测试代码,但这是我通常发出cURL请求的方式。

如果你说如果你访问页面时直接在地址栏中输入所有参数(如GET参数),那么这意味着你不需要进行POST调用

在这种情况下,您甚至不需要使用cURL:

$base='1!'http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx';
$params=数组(
“用户名”=>$username,
“密码”=>$password,
'至'=>$diner\u mobile,
'text'=>'您的预订已确认',
);
$url=sprintf('%s?%s',$base,http_build_query($params));
$response=file\u get\u contents($url);
但是,如果您确实需要使用POST,这应该可以:

$curl=curl_init();
curl_setopt_数组($curl,数组(
CURLOPT_URL=>$base,
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>$params,
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_SSL_VERIFYHOST=>0,//如果需要从https获取,可以避免SSL问题
CURLOPT_SSL_VERIFYPEER=>0,//相同^
));
$response=curl\u exec($curl);

注意:我显然没有测试代码,但这是我通常发出cURL请求的方式。

很抱歉,我的代码$param=$param中漏掉了这一行$短信;抱歉,我的代码$param=$param中遗漏了这一行$短信;谢谢,让我检查一下这个片段。谢谢,让我检查一下这个片段。