Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用-d命令执行Curl请求_Php_Curl_Libcurl - Fatal编程技术网

Php 使用-d命令执行Curl请求

Php 使用-d命令执行Curl请求,php,curl,libcurl,Php,Curl,Libcurl,我想用-d执行curl命令,以使用Africa Talking API发送短信。不幸的是,我无法从服务器获得任何响应,而在响应正文中有false请帮助我如何发送请求。我是curl的新手 这是来自 据我所知,以下是我实施的内容 $messages = array( 'username'=>'sandbox', //rather my username 'to'=>$phone, // 266XXXXXXX,266XXXXXX 'message'=>$tex

我想用
-d
执行curl命令,以使用Africa Talking API发送短信。不幸的是,我无法从服务器获得任何响应,而在响应正文中有
false
请帮助我如何发送请求。我是curl的新手

这是来自

据我所知,以下是我实施的内容

$messages = array(
    'username'=>'sandbox', //rather my username
    'to'=>$phone, // 266XXXXXXX,266XXXXXX
    'message'=>$text,//Hello
    'from'=>$from //Sandbox
);

$url = "https://api.sandbox.africastalking.com/version1/messaging";
//$url="https://api.africastalking.com/version1/messaging";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/x-www-form-urlencoded',
    'apiKey:my-api-key'
));

curl_setopt($ch, CURLOPT_POSTFIELDS, ($messages)); //json_encode($messages)

$server_output = curl_exec($ch);
curl_close($ch);

var_dump($server_output);

输出
false

Curl可以……我认为问题在于非洲跟踪文档。然而,这对我起了作用

curl-X POSThttps://api.sandbox.africastalking.com/version1/messaging -H'Accept:application/json'-H'内容类型:application/x-www-form-urlencoded'-H'apiKey:myapppikey'-d username=MyAppUsername&to=%2B254711XXXYYY,%2B254733YYYYZZ&message=Hello%20World&from=myShortCode'
如果您不使用沙盒应用程序,则应使用实时版本,如下所示

curl-X POSThttps://api.africastalking.com/version1/messaging -H'Accept:application/json'-H'内容类型:application/x-www-form-urlencoded'-H'apiKey:myapppikey'-d username=MyAppUsername&to=%2B254711XXXYYY,%2B254733YYYYZZ&message=Hello%20World&from=myShortCode'

您需要构建一个查询字符串,以便将数据成功发送到端点。这意味着改变

curl_setopt($ch, CURLOPT_POSTFIELDS, ($messages)); //json_encode($messages)


curl命令行命令有效吗?当curl_exec()返回False时,意味着无法发送请求,因此根本没有任何服务器响应。我测试了您的代码。它表示
找不到请求的资源。
表示URL无效?@Stack:代理、网络问题或其他问题。正如Amanjot所说,您的代码工作并返回字符串,而不是False。因此,如果在您的计算机上,这个精确的代码返回False,那么它不是代码,很可能是连接。
curl_setopt($ch, CURLOPT_POSTFIELDS, ($messages)); //json_encode($messages)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($messages)); //json_encode($messages)