mySQL 别名字段名称在 curl_exec PHP 调用中失败

mySQL with alias field names failing in curl_exec PHP call

我有一个使用 curl_exec.

调用 PHP 数据脚本的简单方法
function load_entry_list($url) 
{       
    // Initialize an empty array to store entries 
    $entries = array(); 

    // Load the data from the API 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $ch_data = curl_exec($ch); 
    curl_close($ch);        

    ***point of failure*** 
    if(!empty($ch_data)) // 
    { 
       // Convert the JSON into an array of entry objects
       $json_data = json_decode($ch_data, TRUE);
       foreach($json_data as $entry)
       {
          $entries[] = (object) $entry;
       }
       return $entries; 
    } 
    else 
    { 
       die('Something went wrong with the API request!'); 
    } 
}

PHP 脚本如下所示

// Create connection
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);

// Check connection
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT rec.id, rec.name, rec.submitted_by, rec.addDate, rec.hits, rec.metaDesc, rec.metaKeys, 
              IFNULL(pic.picPath,'default.png') as picPath, 
              var.catname as variety, cat.catname as category
        FROM wms_recipes rec LEFT OUTER JOIN 
             wms_categories var on rec.cat = var.id LEFT OUTER JOIN
             wms_categories cat on var.childOf = cat.id LEFT OUTER JOIN
             wms_pictures pic on rec.id = pic.recipe
       WHERE enRecipe  = 'yes'
             AND isApproved  = 'no'
       ORDER BY 1";

$result = $conn->query($sql);   

$rows = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {              
        $rows[] = $row;
    }
} 
$conn->close();

$articles = array();
foreach($rows as $item) {           
    $myArticle = new article();
    $myArticle->id = $item['id'];       
    $myArticle->name = $item['name'];
    $myArticle->category = $item['category'];  *** it will also work if I comment out this line
    $myArticle->variety = $item['variety'];
    $myArticle->submitted_by = $item['submitted_by'];
    $myArticle->addDate = $item['addDate'];
    $myArticle->hits = $item['hits'];
    $myArticle->description = $item['metaDesc'];
    $myArticle->keys = $item['metaKeys'];
    $myArticle->picPath = IMAGES_URL.'recipe/'.$item['picPath'];
    $articles[] = $myArticle;
}

header('Content-Type: application/json');   
echo json_encode( $articles );
exit;

更新: 这是问题,

如果我在 SQL 查询中保留 cat.catname as category

然后在我的调用方法中 $ch_data$ch_data = curl_exec($ch); 将是 empty

原来是一个类别导致了这个问题。这是罪魁祸首的一个例子:

[11] => article Object
    (
        [id] => 12
        [name] => White Zinfandel from Fresh Juice
        [category] => Rosé Wines
        [variety] => Zinfandel
        [submitted_by] => Jim Stevens
        [addDate] => 2016-04-27
        [hits] => 558
        [description] => A good gift wine that all will like
        [metaKeys] => 
        [picPath] => http://localhost/resources/images/recipe/default.png
        [keys] => White Zinfandel
    )

记下类别名称 桃红葡萄酒。尖锐的 é 导致了这个问题。如果我拼写它上升,问题就消失了。 SQLreturns就好了。但是curl_exe不喜欢。所以现在我的问题是为什么以及如何解决它。

解决此问题的最简单方法是像这样强制 mysql 连接到 utf-8:

$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME_WMS); $conn->set_charset("utf8");