0

嗨,我正在尝试使用 POST 方法发送这个 json 对象,但我不确定如何包含下面的标题,我检查了很多示例,但没有人使用下面列出的标题。

这是我的 php 处理页面的代码,该页面将处理从 form.html 接收到的输入,并将创建要传输到 http 服务器(外部平台)的 json 对象

<?php 
$host ="10.10.237.8"; 
$puerto = 21098; 
$BUFF = 2048; 
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
$conexion = socket_connect($socket, $host, $puerto); 
if($conexion) 
{ 
    $msisdn=$_POST["msisdn"]; 
    $mensaje=$_POST["mensaje"]; 
    $php_array = array( 
        "numero" => $msisdn, 
        "mensaje" => "$mensaje", 
    ); 
    $json_array=json_encode($php_array); 
    echo "Conexion Exitosa, puerto: " . $puerto."\n\n"; 
    echo $json_array; 
    socket_write($socket, $json_array); 
    // Receive the results (if any) from the server 
    $server_response = socket_read($socket, $BUFF); 
    $decoded_response = json_decode($server_response, true); // decode the data received 
    echo $decoded_response['passphrase']; // and spit the results 
} 
else 
{ 
    echo "\nLa conexion TCP no se pudo realizar, puerto: ".$puerto; 
} 
socket_close($socket); 
?>

这些是标题信息,我应该在哪里包含它们?怎么做 ?使用卷曲?你有一个很好的例子吗?

POST /SMBULK/BATCH HTTP/1.0
Authorization: Basic dGVzdDp0ZXN0
Host: 10.10.237.8:21098
Content-Length: 395
User-Agent: Wget/1.12 (solaris2.10)
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Accept: */*

我希望你能帮助我。上面的 php 代码不起作用,因为某些信息(POST、Content-Length、Content-Type、User-Agent...等)未包含在内(包含的“主机”除外)。

我设法做到了,但它不起作用:

<?php
$str_obj_json='{
"method":"SUBMIT","params":{
"batchType":"submit",
"batchId":"alvarons",
"origAddr":"550",
"origTon":2,
"userData":"Movistar les desea Feliz Navidad",
"submits":
 [
  {
  "messId":"mess127_001",
  "destAddr":"51975375377"}
 ]
  }
}';
$ch = curl_init('http://10.10.237.8:21098/SMBULK/BATCH');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_obj_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: 395',
'Authorization: Basic dGVzdDp0ZXN0',
'User-Agent: Wget/1.12 (solaris2.10)',
'Connection: Keep-Alive',
'Accept: */*')
);
$result = curl_exec($ch);
?>

怎么了 ?

4

1 回答 1

0

要使用 cUrl 发送 json 对象,您必须将“Content-Type: application/json”添加到标题

curl_setopt($curl, CURLOPT_HTTPHEADER,
                array(
'Content-type: application/json'
'Content-Length: 395',
'Authorization: Basic dGVzdDp0ZXN0',
'User-Agent: Wget/1.12 (solaris2.10)',
'Connection: Keep-Alive',
'Accept: */*');
于 2013-01-22T09:29:27.717 回答