簡體   English   中英

使用PHP cURL發布JSON數據

[英]Posting JSON Data With PHP cURL

情況

  • 我想使用php cURL將JSON數據發布到我的URL http://localhost/api_v2/url?key=***

這是我嘗試過的

開機自檢功能

public function post(){

    $ch = curl_init("http://localhost/api_v2/url?key=***");

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $file_name = 'inventory.csv';
    $file_path = 'C:\\QuickBooks\\'.$file_name;
    $csv= file_get_contents($file_path);
    $utf8_csv = utf8_encode($csv);
    $array = array_map("str_getcsv", explode("\n", $utf8_csv));
    $json = json_encode($array, JSON_PRETTY_PRINT);

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

    curl_setopt( $ch, CURLOPT_POSTFIELDS, $json ); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));

    $result = curl_exec($ch);

    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($status == 200){
        echo "Post Successfully!";
    }
}

您的錯誤在這一行

curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));

只需將其更改為

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

讓我知道事情的后續 !

我不熟悉laravel,

但是你不需要添加行

curl_setopt( $ch, CURLOPT_POSTFIELDS, $json );

之前

curl_exec($ch)?

暫無
暫無

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

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