curl -X POST -H'Content-Type:application / json'-d to PHP [英] curl -X POST -H 'Content-Type: application/json' -d to PHP

查看:4031
本文介绍了curl -X POST -H'Content-Type:application / json'-d to PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个curl请求,我有这行curl -X POST -H'Content-Type:application / json'-d,需要翻译到PHP curl。问题是我不知道-X,-H和-d是什么意思。

I need to make a curl request, I have this line "curl -X POST -H 'Content-Type: application/json' -d" and need to "translate" to PHP curl. The problem is I don't know what the "-X", "-H" and "-d" mean.

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Content-Type: application/json',
        'Content-Length: '. strlen($itemJson))
    );

我在标题上尝试过类似的东西( $ itemJson 是一个JSON字符串),但我有错误400。

I tried something like that on header ($itemJson is a JSON string) but I got error 400.

我想我以错误的方式请求。任何人都可以帮助我?

I think I'm doing the request in a wrong way. Can anybody help me?

推荐答案

您可以尝试如下

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://somedomain.com/test.php');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

这篇关于curl -X POST -H'Content-Type:application / json'-d to PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆