如何使用curl和php将json数据发布到api数据库

时间:2016-02-20 05:06:29

标签: php json curl ftp

当我试图将数据发布到第三方api数据库时,我正在努力理解这个错误。

我在这里有$数据,我想用curl发布,它是json格式。

<?php

$url = 'https://www.yourdomain.com/uploadproducts';
$api_key = 'nMUiKg/oj7xtq40'; 
$sec_key = 'Nf+NQ2/WyjohqXOF1';

    $data = '{"product_sku":"FLAFFA000","product_category":"Fifth Avenue","product_subcategory":"0","product_name":"Love Print Flats","product_url":"http://love-print-flats-857","product_description":"Solid Candy Color Cartoon Printed Big","price":"1850","age_group":"0","product_img":"media/catalog/product/f/l/flaffa0001pin38.jpg,media/catalog/product/f/l/flaffa0001pin38_a.jpg,media/catalog/product/f/l/flaffa0001pin38_b.jpg","product_stock":[{"38":"1.0000"}],"product_discount":"100","discount_start_date":"0","discount_end_date":"0","product_tags":["0"]}';


    //var_dump($data);

    $data_string ='scapi_key='.urlencode($api_key).'&scsecret_key='.urlencode($sec_key)."&products=".urlencode($data);

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);                                                                     
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    $response = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'Curl error: ' . curl_error($ch);
    }
      curl_close($ch);
        $arr = json_decode($response, true);
    echo "<pre>$response</pre>";
        echo $arr;    
    ?>

当我尝试使用网址www.yourdomain.com/test.php执行此文件时,我收到以下错误。

{"msg":"200 OK","success":"Successfully added 0","updated":"Successfully updated 0","error":"Errors found 14"}
Array

然后我尝试使用

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

到我上面的代码,我收到以下错误

{"msg":"API key is missing","success":"Successfully added 0","updated":"Successfully updated 0"}
Array

我还转储了json数据并验证了json格式,

string(943) "{"product_sku":"FLAFFA000","product_category":"Fifth Avenue","product_subcategory":"0","product_name":"Love Print Flats","product_url":"http://love-print-flats-857","product_description":"Solid Candy Color Cartoon Printed Big","price":"1850","age_group":"0","product_img":"media/catalog/product/f/l/flaffa0001pin38.jpg,media/catalog/product/f/l/flaffa0001pin38_a.jpg,media/catalog/product/f/l/flaffa0001pin38_b.jpg","product_stock":[{"38":"1.0000"}],"product_discount":"100","discount_start_date":"0","discount_end_date":"0","product_tags":["0"]}";

关于这个问题的更多研究,我已经阅读了卷曲代码的文件,它说错误14是 CURLE_FTP_WEIRD_227_FORMAT(14) FTP服务器返回227行作为对PASV命令的响应。如果libcurl无法解析该行,则返回此返回码。

因此,根据显示的错误,是否有任何可能的解决方案卷曲错误14或代码中是否有错误? FTP 会出现什么问题?

任何一点帮助或提示都会帮助我。

0 个答案:

没有答案