简体   繁体   English

Curl 请求到 PHP Curl 请求

[英]Curl request to Php Curl Request

I need a help rewriting this request to php curl:我需要帮助将此请求重写为 php curl:

curl -d '{"text":"Hello,
#param#.","port":[2,3],"param":[{"number":"123456","text_param
":["John"],"user_id":1},{"number":"123478",
"text_param":["Sam"],"user_id":2}]}’ –H "Content-Type:
application/json" http://gateway_ip/api/send_sms

So far i didnt have any success.到目前为止,我没有任何成功。 here is what i tried:这是我尝试过的:

$url = "http://url/api/send_sms";

$params = array(
    'auth' => 'user:pass',
    'port' => 7,
    'text' => utf8_encode('Hello, world!')
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

What I am doing wrong?我做错了什么?

Something like this:像这样的东西:

<?php
$ch = curl_init();

$json = <<<JSON
{"text":"Hello,
#param#.","port":[2,3],"param":[{"number":"123456","text_param
":["John"],"user_id":1},{"number":"123478",
"text_param":["Sam"],"user_id":2}]}
JSON;

curl_setopt($ch, CURLOPT_URL, "http://gateway_ip/api/send_sms");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Build $json var by your own, but create a valid json with json_encode function自己构建 $json var,但使用json_encode函数创建一个有效的 json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM