簡體   English   中英

使用 PHP cURL POST JSON

[英]POST JSON with PHP cURL

我有以下php代碼

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_headers);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_VERBOSE, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,  120);
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_POSTFIELDS, '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}');
curl_setopt($ch, CURLOPT_POST, 1);

但我不明白為什么不起作用。 我發布 JSON 的 API 表示未收到參數。 我的代碼有什么問題嗎? 我認為整個技巧都在 JSON 參數上......我不知道如何發送它們,因為我看不到任何帶有 http 分析器的“nave->value”對,因為它通常以簡單的形式出現......只是沒有任何“名稱”的 JSON 代碼。

您可以嘗試如下。 基本上,如果我們有一個數據數組,那么它應該是使用 php json_encode進行json encoded 並且您還應該在header添加content-type ,它可以在 curl 中定義為curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$data = array("country"=>"US","states"=>array("MHASASAS"=>"MH","XYZABABA"=>"XYZ"));
$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);

您可以使用它並替換為

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}');

用這個

$Parameters = array(
    'MerchantCode'        => $MerchantCode,
    'PriceValue'          => $amount,
    'ReturnUrl'           => $callback,
    'InvoiceNumber'       => $resnum,
);

    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($Parameters));

如果:您使用 post 方法,您應該知道:

CURLOPT_POST TRUE 執行常規 HTTP POST。 此 POST 是普通的 application/x-www-form-urlencoded 類型,最常用於 HTML 表單。

@see http://php.net/manual/en/function.curl-setopt.php

所以:你可以這樣做:

$ar_form = array('name'=>'PHPJungle','age'=>66,'gender'=>'male');
$poststr = http_build_query($ar_form ); # important

$options[CURLOPT_HTTPGET] = false;
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $poststr ; //default type:application/x-www-from-urlencoded

curl_setopt_array ( $ch, $options );
# @todo your other codes

這是我用了很久的類。該類基於PHP cURL。 支持GET/POST,HTTP/HTTPS。

@see https://github.com/phpjungle/iHttp/

您可以像這樣使用 curl 發布 json 數據:

使用命令提示符:

    curl -X POST -H "Content-Type: application/json" -d '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}' $url

使用 PHP:

    $data = array("folderId"=>"1","parameters"=>array("amount"=>3,"ascending"=>false,"offset"=>0,"sort"=>"date"));
    $postdata = json_encode($data);
    OR
    $postdata = '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}';
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);
    curl_close($ch);

    print_r($result);
$link = "http://test.domain/myquery";
   $json = '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}';
   $postdata = json_decode($json);
 echo openurl($link, $postdata);

這在 json decode 將 json 字符串轉換為數組時起作用。

function openurl($url, $postvars = "") {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        $content = trim(curl_exec($ch));
        curl_close($ch);
        return $content;
    }

如果您的 API 端點使用 body 發送使用 json 數據的請求可能是您可以使用Guzzle文檔在這里doc

use GuzzleHttp\Client;
$client = new Client();
$request = $this->client->post($url,array(
            'content-type' => 'application/json'
    ),array());
$request->setBody($json_data);
$response = $request->send();
return $response;

希望這項工作。

您尚未設置內容類型,因此發布數據將作為表單數據發送。 嘗試將內容類型設置為 application/json。

如果這不起作用,請嘗試用數組包裝 json 字符串。

我不確定,這是解決方案,但這在發布 json 時對我有用,從

'{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}'

"{'folderId':'1','parameters':{'amount':3,'ascending':false,'offset':0,'sort':'date'}}"

我所做的唯一改變是雙引號現在在外面,這對我有用,但我顯然要發布到不同的服務器

我能提供的唯一幫助是下載網絡調試工具,例如FiddlerCharles代理並監控發送/接收的請求,這可能是您的代碼中出現其他錯誤的情況。

希望我有所幫助:)

您可以通過以下步驟進行制作:

$data = array(
    'folderId'=>"1","parameters"=>array(
        "amount"=>"3","ascending"=>false,"offset"=>0,"sort"=>"date"
    )
);

$data_string = http_build_query($data);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

$result = json_decode($result,true);

我不知道你是否需要標題。 我認為默認情況下它已經是application/x-www-form-urlencode

如果它不起作用,請嘗試更改數組中的$data值。 認為它有幫助。 :)

首先請檢查 curl http 狀態碼

$http_code= curl_get_info($exec_res,CURL_HTTP_CODE);

然后使用 post header API 服務器修改此請求標頭集,添加一個記錄器來記錄這些請求信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM