如何使用PHP cURL发布JSON数据?
这是我的密码如何使用PHP cURL发布JSON数据?,php,json,rest,curl,Php,Json,Rest,Curl,这是我的密码 $url = 'url_to_post'; $data = array( "first_name" => "First name", "last_name" => "last name", "email"=>"email@gmail.com", "addresses" => array ( "address1" => "some address", "city" => "city",
$url = 'url_to_post';
$data = array(
"first_name" => "First name",
"last_name" => "last name",
"email"=>"email@gmail.com",
"addresses" => array (
"address1" => "some address",
"city" => "city",
"country" => "CA",
"first_name" => "Mother",
"last_name" => "Lastnameson",
"phone" => "555-1212",
"province" => "ON",
"zip" => "123 ABC"
)
);
$data_string = json_encode($data);
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string)
)
);
$result = curl_exec($ch);
curl_close($ch);
在另一页,我正在检索post数据
print_r ($_POST);
输出为
HTTP/1.1 200 OK
Date: Mon, 18 Jun 2012 07:58:11 GMT
Server: Apache
X-Powered-By: PHP/5.3.6
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
Array ( )
所以,即使在我自己的服务器上,我也无法获得正确的数据,因为它是空数组。我想在Replace使用json实现REST
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
与:
我不明白你所说的“其他页面”是什么意思,我希望它是位于“url\u to\u post”的页面。如果该页面是用PHP编写的,您刚才发布的JSON将按以下方式读取:
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
试着这样做:
$url = 'url_to_post';
// this is only part of the data you need to sen
$customer_data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
// As per your API, the customer data should be structured this way
$data = array("customer" => $customer_data);
// And then encoded as a json string
$data_string = json_encode($data);
$ch=curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Content-Length: ' . strlen($data_string)))
));
$result = curl_exec($ch);
curl_close($ch);
$url = 'url_to_post';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
$data_string = json_encode(array("customer" =>$data));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo "$result";
您忘记的关键是对数据进行json_编码。但您也可能会发现,通过传递数组,使用curl_setopt_数组可以方便地同时设置所有curl选项 试试这个例子
<?php
$url = 'http://localhost/test/page2.php';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
$ch=curl_init($url);
$data_string = urlencode(json_encode($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
您的page2.php代码
<?php
$datastring = $_POST['customer'];
$data = json_decode( urldecode( $datastring));
?>
您发布的json不正确——但即使它是正确的,您也无法使用
print\r($\u POST)
()进行测试。相反,在第二页上,您可以使用文件获取内容(“php://input”
,其中将包含发布的json。要以更可读的格式查看收到的数据,请尝试以下操作:
echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';
";
旁注:您可能会从使用而不是直接与Shopify API交互中获益。请尝试以下代码:-
const CAINFO = SERVER_ROOT . '/registry/cacert.pem';
...
\curl_setopt($ch, CURLOPT_CAINFO, self::CAINFO);
这个代码对我有用。你可以先试试…
,它“检查公共名称是否存在,并验证它是否与提供的主机名匹配”,以及CURLOPT_SSL_VERIFYHOST==2
它“验证对等方的证书”CURLOPT_VERIFYPEER==true
$fields = array('a' => 'sth', 'b' => 'else');
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_POST, 1);
\curl_setopt($ch, CURLOPT_POSTFIELDS, \http_build_query($fields));
取自一个工作类,其中SERVER\u ROOT
是在应用程序引导期间定义的常量,如在自定义类加载器、另一个类等中
忘记像这样的事情\CURLOPT\u setopt($handler,CURLOPT\u SSL\u VERIFYHOST,0);
或\curl\u setopt($handler,CURLOPT\u SSL\u VERIFYPEER,0);
查找相关的cacert.pem
2个POST模式
发布数据时,实际上有两种模式:
- 传输数据时,将
标题设置为内容类型
或多部分/表单数据
- 数据是带有
编码的URL编码字符串application/x-www-form-urlencoded
多部分/表单数据
示例:
应用程序/x-www-form-urlencoded
示例:
http\u build\u query
:
在命令行中测试它,如下所示
POST请求的另一端将定义适当的连接模式。您是否缺少使用
json\u encode()
?将$data
转换为$data\u string
的功能?没有看到这行代码…抱歉,我没有在这里写,但在我的代码中我写了code
$data\u string=json\u encode($data);code
以及如何在注释中编写代码?在注释中,我不能写换行符,因此如何编写代码?-1.检查此处的API:。服务器期望的主体是JSON,而不是URLCoded JSON。检查我的答案,不需要在'CURLOPT_postfieldsys中使用array(…)
,因为我说他发送的是错误的。传递数组(…)
to CURLOPT_POSTFIELDS`也会对JSON进行URL编码。无论如何,我用不同的代码尝试了很多次,但都没能用JSON,现在我用xml成功地做到了。你为什么会这样认为?如果他把数据放在“客户”字段中,他这样做一定是有原因的,不是吗?是的,谢谢,我错过了这一部分。但是他,依我看,是做错了。我将用它更新我的答案。上述解决方案都无法在php文件中获取json数据:(所以你不必显式地指定它是POST请求?这是众所周知的,因为设置了CURLOPT_POSTFIELDS?当我上周整个星期都在寻找它的时候,这个答案在哪里?现在我必须弄清楚自己的想法后才发现它!旁注:如果你发送JSON并期望JSON作为响应,那么一些API也需要设置响应类型curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json','Accept:application/json'));
(否则您可以发送json,但可以获取XML作为答案)。
$url = 'url_to_post';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
const CAINFO = SERVER_ROOT . '/registry/cacert.pem';
...
\curl_setopt($ch, CURLOPT_CAINFO, self::CAINFO);
$fields = array('a' => 'sth', 'b' => 'else');
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_POST, 1);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$fields = array('a' => 'sth', 'b' => 'else');
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_POST, 1);
\curl_setopt($ch, CURLOPT_POSTFIELDS, \http_build_query($fields));
user@group:$ php -a
php > $fields = array('a' => 'sth', 'b' => 'else');
php > echo \http_build_query($fields);
a=sth&b=else