PHP cURL, POST JSON

11

我需要提交以下JSON代码,但出现了问题。下面是我的代码。

$fieldString = "395609399";
//the curl request processor
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    if ($fieldCount) { // in case of post fields present then pass it on
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}"));
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    $resulta = curl_exec($ch);
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
    }
    return $resulta;
}

以下是调用cURL请求的函数:

function get_cat($categoryId, $URL) {
    $fields = array(
        "categoryId" => $categoryId
    );
    $fields_string = $fields;
    return $this->processCurlJsonrequest($URL, $fields_string);
}

1
给json_encode一个PHP数组,而不是已经是JS对象形式的字符串。 - JAL
我已经尝试了 curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_encode(array( "categoryId"=>"5016")))); 和 json_encode(array( "categoryId"=>"5016"))); 但都没有起作用。 - Michael
3
很烦人的是,你显然已经解决了问题(根据你的评论),但没有更新帖子说明解决方案是什么。 - shanusmagnus
5个回答

19

有问题的比特是:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}"));

CURLOPT_POSTFIELDS可以接受参数数组或URL编码的参数字符串:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($stuff)));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode(json_encode($stuff)));

其中json将是POST字段的名称(即将导致$_POST['json']可访问)。


由于某些原因无法正常工作。我收到了{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}Array ( [Message] => There was an error processing the request. [StackTrace] => [ExceptionType] => ) 的错误信息。我认为这是因为提交不正确导致的。 - Michael
我已经在此链接上上传了一张图片,这样您就可以看到我正在尝试使用PHP Curl复制的HTTP分析器中的确切POST请求。http://img502.imageshack.us/img502/1346/analyzer.jpg - Michael
2
正如您在HTTP分析器中所看到的,没有出现任何POST字段(例如“json”)。 - Michael
1
@Michael,你所访问的服务器设置了一项安全功能,不接受由POSTFIELDS生成的普通PHP POST请求。你需要将代码主体放入一个文件中,并通过PHP上传该文件。我之前也遇到过类似的问题。 - James Wayne

9

发送文本时不需要进行JSON编码,只需添加以下标头代码:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01")); 

7
使用初始示例,工作代码应该像这样: ```html

使用初始示例,工作代码应该像这样:

```
//the curl request processor
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("myJsonData" => "test")));
    curl_setopt($ch, CURLOPT_POST, 1); 
    $resulta = curl_exec($ch);
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
    }
    return $resulta;
}

4

我在使用cURL发送JSON时遇到了问题,问题是我没有在头部明确设置内容长度。

因此,头部应该是:

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

3

实际上很简单,确保你设置了额外的 Content-Type 头部为 json,然后 CURLOPT_POSTFIELDS 可以以字符串形式接受 json。不需要进行编码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接