0

得到:可捕获的致命错误:无法将类 stdClass 的对象转换为字符串...从以下代码:

$url = "{$row['url']}";

$embed_info = json_decode($client->get('oembed', array('url' => $url)));
$sql="INSERT INTO persons (iframe) VALUES('$embed_info')";

如何让 $embed_info 作为字符串工作并很好地进入我的数据库?

4

2 回答 2

0

看着这个:

$embed_info = json_decode($client->get('oembed', array('url' => $url)));

假设$client->get调用返回 JSON(一个字符串),那可能就是您想要存储的内容。一旦你调用json_decode那个 JSON,$embed_info它就是一个对象(解码的 JSON)。然后你不能做类似的事情,$sql = "...$embed_info...";因为 PHP 会尝试隐式尝试将Object转换为 String。

如果您只想存储 JSON,请跳过json_decode调用。

于 2013-02-27T21:39:17.837 回答
0

不确定是一个好的做法,或者它是否符合您的需要,但您可以序列化对象并使用 base64 对其进行编码(用于逗号和其他字符)

$embed_info = json_decode($client->get('oembed', array('url' => $url)));
$data = base64_encode(serialize($embed_info));
$sql = "INSERT INTO persons (iframe) VALUES ('{$data}')";

稍后您可以检索该对象:

$sql = "SELECT iframe FROM persons WHERE <the where>";
// ... execute the query and fetch in $row
$embed_info = unserialize(base64_decode($row['iframe']));
// Now in $embed_info you get back the object 
// Note that the object's class must be loaded before do that
于 2013-02-27T21:50:04.827 回答