简体   繁体   English

无法在PHP中将JSON对象插入MySQL表

[英]Trouble inserting JSON object into MySQL table in PHP

I am calling a Rest API in my PHP code which returns JSON. 我正在我的PHP代码中调用Rest API,该API返回JSON。 I want to save some parts of that into my database but for some reason, nothing is being stored. 我想将其中的某些部分保存到我的数据库中,但是由于某种原因,什么也没有存储。 Here is what the JSON should look like: JSON如下所示:

{
"url": "http://myURL.com/fujifilm-mx1700.jpg",
"mimeType": "image/jpeg",
"width": 640,
"height": 480,
"byteSize": 100227,
"exif": {"YResolution": "72/1", "Tag0xa217": "2", "ResolutionUnit": "Inch", "Compression": "6", "Copyright": "", "Tag0xa300": "[ 3 ]", "Make": "FUJIFILM", "Flash": "no", "DateTime": "2000:09:02 14:30:10", "MaxApertureValue": "3.3", "YCbCrPositioning": "2", "XResolution": "72/1", "JPEGInterchangeFormatLength": "4354", "ExposureBiasValue": "0.0", "ExposureProgram": "Program Normal", "ShutterSpeedValue": "1/169", "ColorSpace": "1", "Tag0xa20e": "1087/1", "Tag0xa20f": "1087/1", "ExifImageWidth": "640", "DateTimeDigitized": "2000:09:02 14:30:10", "DateTimeOriginal": "2000:09:02 14:30:10", "BrightnessValue": "76/10", "CompressedBitsPerPixel": "2/1", "Interoperability_IFD_Pointer": "708", "FNumber": "7", "ApertureValue": "5.6", "Tag0xa210": "3", "FocalLength": "9.9", "Tag0xa000": "[ 48,49,48,48 ]", "ComponentsConfiguration": "[ 1,2,3,0 ]", "ExifImageHeight": "480", "ISOSpeedRatings": "125", "Model": "MX-1700ZOOM", "Software": "Digital Camera MX-1700ZOOM Ver1.00", "Orientation": "1", "Tag0xa301": "[ 1 ]", "JPEGInterchangeFormat": "856", "MeteringMode": "5", "ExifVersion": "[ 48,50,49,48 ]"}
}

And here is my PHP 这是我的PHP

$exif = file_get_contents('http://img2json.appspot.com/go/?url=http://myURL.com/$fileName');
$response = json_decode($response, true);

foreach($response['exif'] as $item) {

$iso = $item['ISOSpeedRatings'];
$shut = $item['ShutterSpeedValue'];

$sql="INSERT INTO EXIF (uniqueID, ISO, shutterSpeed)
VALUES('$id','$iso','$shut')";  

    if (!mysql_query($sql,$link)) {
    die('Error: ' . mysql_error());
    }
}

I want to store ISOSpeedRatings and ShutterSpeedValue in my database, but it they keep coming up empty. 我想将ISOSpeedRatings和ShutterSpeedValue存储在我的数据库中,但是它们一直空着。

I suspect its not working because I am not properly retrieving the data I want from the JSON, but I am not too sure what to do. 我怀疑它无法正常工作,因为我没有从JSON正确检索想要的数据,但是我不太确定该怎么做。 Also, I think its worth noting that I am using AppFog so I had to make sure I had php_value allow_url_fopen 1 in my .htaccess, since I have no access to the php.ini file. 另外,我认为值得一提的是,我正在使用AppFog,因此必须确保我的.htaccess中有php_value allow_url_fopen 1 ,因为我无法访问php.ini文件。 Any help would be appreciated. 任何帮助,将不胜感激。

PHP string interpolation (the thing that makes it replace variable names with their values in a string) only works (as far as I know) in a double-quoted string. PHP字符串插值(使变量名称替换为字符串中的值的事情)(就我所知)仅在双引号字符串中有效。 If I'm reading it right, and my brain still works this late at night, your problem is here: 如果我没看错,但是我的大脑在深夜仍然可以正常工作,那么您的问题就在这里:

    $exif = file_get_contents('http://img2json.appspot.com/go/?url=http://myURL.com/$fileName');

Because you are literally requesting the file '$filename ' rather than whatever is actually in that variable. 因为您实际上是在请求文件'$filename ',而不是该变量中的实际内容。 Replace the single-quotes with double-quotes and see if that fixes it. 用双引号替换单引号,然后查看是否可以解决。

Update: next problem: 更新:下一个问题:

    $response = json_decode($response, true);

You need to decode the $exif variable. 您需要解码$exif变量。

尝试使用urlencode()准备您的urlstring。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM