简体   繁体   中英

Trouble inserting JSON object into MySQL table in PHP

I am calling a Rest API in my PHP code which returns 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:

{
"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

$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.

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. 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. 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. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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