【问题标题】:PHP cURL, POST JSONPHP 卷曲,POST JSON
【发布时间】:2011-05-15 08:54:35
【问题描述】:

我需要发布以下 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);
}

【问题讨论】:

  • 给json_encode一个PHP数组,而不是一个已经是JS对象形式的字符串。
  • 我试过 curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_encode(array( "categoryId"=>"5016"))));和 json_encode(array("categoryId"=>"5016")));并且也不工作
  • 您显然解决了问题(如您的评论所示)但没有更新帖子以说明解决方案是什么,这真是太烦人了。

标签: php curl libcurl


【解决方案1】:

问题在于:

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] => ) 。我认为是因为帖子没有正确发布。
  • 我已经在这个链接上上传了一张图片,这样你就可以看到我试图用 php curl 复制的来自 http 分析器的确切发布请求。 img502.imageshack.us/img502/1346/analyzer.jpg
  • 正如您在 http 分析器中看到的,没有出现任何帖子字段(例如“json”)
  • @Michael 您发布的服务器设置了不接受 POSTFIELDS 生成的普通 PHP 发布请求的安全功能。您需要将正文代码放在一个文件中并通过 php.ini 上传该文件。我遇到了类似的问题。
【解决方案2】:

发送不带 JSON 编码的文本,并添加此标头代码:

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

【讨论】:

    【解决方案3】:

    对于最初的例子,工作代码应该是这样的:

    //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)));
      

      【讨论】:

        【解决方案5】:

        真的很简单,确保你已经为 json 设置了额外的 Content-Type 标头,然后 CURLOPT_POSTFIELDS 可以将 json 作为字符串。无需编码。

        【讨论】:

          猜你喜欢
          • 2016-08-12
          • 2012-08-13
          • 2015-03-25
          • 2017-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-06
          • 2018-09-20
          相关资源
          最近更新 更多