PHP cURL - json请求的模式不正确

我正在尝试在php中实现以下cURL请求:</ p>

 <  code> curl -X POST \
H“Content-Type:application / json”\
H“授权:登录:密码”\
d'{“phone”:“+ 48667421797”,“msg”:“很棒的应用程序! 谢谢!“}'\
https://some.endpoint.url
</ code> </ pre>

它在控制台中工作正常,我还设法在像这样的工具中使用此请求 postman / insomnia。</ p>

但是当我尝试在 PHP </ code>中实现它时:</ p>

  $ fields  = [
'电话'=&gt; $ number,
'msg'=&gt; $ msg,
];

$ data = json_encode($ fields);

$ headers = [
'Content-Type:application / json',
'授权:登录:密码'\ n];

$ ch = curl_init($ url);

curl_setopt($ ch,CURLOPT_POST,1);

curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

curl_setopt($ ch,CURLOPT_URL,config('kajaki.sms_url'));
curl_setopt ($ ch,CURLOPT_RETURNTRANSFER,1);

$ response = curl_exec($ ch);

curl_close($ ch);
</ code> </ pre>

我得到AFAIK指示的 400:不正确的架构</ code>,该请求未作为有效的JSON请求发送。 </ p>

出了什么问题? 如何以JSON格式正确发送此请求?</ p>

不幸的是,此API不提供任何文档。</ p>
</ div>

展开原文

原文

I'm trying to implement following cURL request in php:

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: login:password" \
-d '{"phone": "+48667421797", "msg": "Great app! Thank you!"}' \
https://some.endpoint.url

It works fine in console, I also managed to use this request in tools like postman/insomnia.

But when I try to implement it in PHP like that:

    $fields = [
        'phone' => $number,
        'msg'   => $msg,
    ];

    $data = json_encode($fields);

    $headers = [
        'Content-Type: application/json',
        'Authorization: login:password'
    ];

    $ch = curl_init($url);

    curl_setopt($ch,CURLOPT_POST, 1);                                                    
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_URL, config('kajaki.sms_url'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);


    $response = curl_exec($ch);

    curl_close($ch);

I get 400: Incorrect schema which AFAIK indicates, that the request is not being sent as valid JSON request.

What is wrong? How can I send this request correctly as a JSON?

Unfortunately this API does not provide any documentation.

drby30217
drby30217 它是正确的。我试过了
2 年多之前 回复
duanbo6871
duanbo6871 尝试打印$data并检查它是否有效json
2 年多之前 回复

2个回答



之前我遇到过类似的问题,我不知道它是否适合您,但您可以尝试 addslashes ()</ code>像这样:</ p>

  $ fields = [
'phone'=&gt; “+48667421797”,
'msg'=&gt; “很棒的应用程序!谢谢!”,
];

$ data = addslashes(json_encode($ fields));

//输出{\“phone \”:\“+ 48667421797 \”,\“msg \”:\“很棒的应用程序!谢谢!\”}
</ code> </ pre>
</ DIV>

展开原文

原文

I've had a similar problem before, I don't know if it will work for you but you can try addslashes() like this:

$fields = [
    'phone' => "+48667421797",
    'msg'   => "Great app! Thank you!",
];

$data = addslashes(json_encode($fields)); 
// outputs {\"phone\":\"+48667421797\",\"msg\":\"Great app! Thank you!\"}

dph6308
dph6308 我不再获得400 /不正确的架构,但服务器响应为空,就好像根本无法识别请求一样。
2 年多之前 回复



您可以编写一个小测试脚本来打印收到的JSON字符串,然后根据测试脚本测试CLI curl命令和您的实现 。 收到的JSON数据是否相同? 如果没有,PHP实现的数据有什么不同?</ p>

index.php </ strong> </ p>

 &lt  ;?php 
var_dump(file_get_contents('php:// input'));
</ code> </ pre>

运行测试网络服务器</ strong> </ p> \ n

  cd目录/包含/ index.php 
php -S localhost:12345
</ code> </ pre>

在CLI上使用cURL发送数据 </ strong> </ p>

  user @ host $ curl -X POST \
&gt; -H“Content-Type:application / json”\
&gt; -H“授权:登录:密码”\
&gt; -d'{“phone”:“+ 48667421797”,“msg”:“很棒的应用程序!谢谢!”}'
n&gt; http:// localhost:12345
string(57)“{”phone“:”+ 48667421797“,”msg“:”很棒的应用程序! 谢谢!“}”
</ code> </ pre>

通过PHP使用cURL发送数据</ strong> </ p>

   //修改URL 
curl_setopt($ ch,CURLOPT_URL,'http:// localhost:12345');
</ code> </ pre>

如果JSON数据相同, 还应该考虑请求的其他部分是否可能无效,例如HTTP标头或URL。</ p>
</ div>

展开原文

原文

You could write a little test script which prints the received JSON string, then test both the CLI curl command and your implementation against the test script. Is the received JSON data the same? If not, what is different in the data from the PHP implementation?

index.php

<?php
var_dump(file_get_contents('php://input'));

Run test webserver

cd directory/containing/index.php
php -S localhost:12345

Send data with cURL on CLI

user@host $ curl -X POST \
> -H "Content-Type: application/json" \
> -H "Authorization: login:password" \
> -d '{"phone": "+48667421797", "msg": "Great app! Thank you!"}' \
> http://localhost:12345
string(57) "{"phone": "+48667421797", "msg": "Great app! Thank you!"}"

Send data with cURL via PHP

// Modify the URL
curl_setopt($ch, CURLOPT_URL, 'http://localhost:12345');

If the JSON data is the same, you should also consider if other parts of the request might be invalid, for example the HTTP headers or the URL.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐