我在Postman工作得很好。使用POST,两个标题(Accept和Content-Type,都是application / json),以及具有此JSON的正文:
{
"conditions": {
"type": "0",
"flag": "0"
}
}
我试图用PHP卷曲模仿这个...但我一直得到一个空结果。这是我迄今为止所做的努力,基于我见过不同地方的事情。
$path = "same/path/as/postman";
$data = array("conditions" => array("type" => 0, "flag" => 0));
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $data ) );
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_URL, $path);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
print("<div>RESULT:</div><pre>");
print_r($result); // null
print("</pre>");
答案 0 :(得分:1)
这是我自己写的卷曲课。我已经使用了很长时间了。希望这会有所帮助。
class httpReq
{
public static $unique = null;
public static function httpGet($url, $params = null, $header = null, $cacheCookie = false)
{
if (!isset(self::$unique)) {
self::$unique = uniqid();
}
if ($params != null && count($params) > 0) {
$url = httpReq::getUrlBuilder($url, $params);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($cacheCookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, sprintf('/tmp/cookie_%s.txt', self::$unique));
curl_setopt($ch, CURLOPT_COOKIEFILE, sprintf('/tmp/cookie_%s.txt', self::$unique));
}
if ($header != null) {
if (is_array($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
$request_headers = array();
$request_headers[] = $header;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
}
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public static function httpPost($url, $params, $header = null, $arrayJsonEncoded = true, $cacheCookie = false)
{
if (!isset(self::$unique)) {
self::$unique = uniqid();
}
if (is_array($params)) {
// Post an array, only values will be url encoded
// JSON Encoding is only available in this mode
$postData = '';
if ($arrayJsonEncoded) {
$postData = json_encode($params);
if ($header == null) {
$header = array();
}
$header[] = 'Content-Type: application/json';
} else {
//create name value pairs separated by & and url encode the values
foreach ($params as $k => $v) {
$postData .= $k . '=' . urlencode($v) . '&';
}
rtrim($postData, '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($cacheCookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, sprintf('/tmp/cookie_%s.txt', self::$unique));
curl_setopt($ch, CURLOPT_COOKIEFILE, sprintf('/tmp/cookie_%s.txt', self::$unique));
}
if ($header != null) {
if (is_array($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
$request_headers = array();
$request_headers[] = $header;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
}
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
} else {
// Post a raw datum
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($cacheCookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, sprintf('/tmp/cookie_%s.txt', self::$unique));
curl_setopt($ch, CURLOPT_COOKIEFILE, sprintf('/tmp/cookie_%s.txt', self::$unique));
}
if ($header != null) {
if (is_array($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
$request_headers = array();
$request_headers[] = $header;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
}
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
private static function getUrlBuilder($baseUrl, $params)
{
$url = $baseUrl;
if ($url[strlen($url - 1)] != '?') {
$url .= '?';
}
$url .= http_build_query($params);
return substr($url, 0, strlen($url) - 1);
}
}