【发布时间】:2015-02-02 20:06:18
【问题描述】:
我正在尝试向 api 发送 POST 请求以使用 PHP cURL 创建用户。这是代码示例
<?php
$email="jas@example.com";
$name = "jas";
$data = array(
"user" => array("email"=>$email,"name"=>$name)
);
//encoding to json format
$jsondata= json_encode($data);
$credentials = "username:pass";
$header[] = "Content-Type: application/json";
$header[] = "Authorization: Basic " . base64_encode($credentials);
$connection = curl_init();
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($connection, CURLOPT_HEADER, false);
//POSTS
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);
curl_setopt($connection, CURLOPT_VERBOSE, 1);
curl_setopt($connection, CURLOPT_URL, "http://domain.freshdesk.com/contacts.json");
$response = curl_exec($connection);
?>
虽然我已经设置了,但看起来它实际上并没有发送帖子
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);
我在 firebug net 选项卡中看到一个 GET 请求。
它真的是一个发布请求吗?因为缩进操作(创建新用户)没有发生,而是列出了所有用户,因为它是一个 GET 请求。
【问题讨论】:
-
你在客户端看不到 curl 请求,你看到的是对服务器端 curl 调用上升的文件的请求
标签: php http post curl firebug